I'm having a problem with tastypie and I just can't find what caused it. Similar question without answers: Tastypie foreign key relationship throwing error
Resource:
class VoteResource(ModelResource):
choice = fields.ToOneField(ChoiceResource, 'choice', full=True)
user = fields.ToOneField(UserResource, 'user')
class Meta:
queryset = Vote.objects.all()
resource_name = 'vote'
'''...'''
always_return_data = True
filtering = {
'id': ALL,
'user': ALL_WITH_RELATIONS,
'choice': ALL_WITH_RELATIONS
}
def hydrate(self, bundle):
bundle.obj.user = bundle.request.user
return bundle
Request payload for creating the object:
{
"choice": "/api/v1/choice/210/"
}
(user is getting added automatically through hydrate). The exception is thrown in ressources.py inside full_hydrate
. According to django console my object is getting loaded correctly.
The line inside tastypie causing this is
setattr(bundle.obj, field_object.attribute, value.obj) # value obj is the evil one
What's killing me is, that it worked like 2 days ago. I added 1 other resource without touching choice, user or any other connected with the model. I checked the recent commit history and the resource is untouched.
Debugged my way through the tastypie source and solved my problem.
Looks like tastypie is calling dehydrate on related objects first. Because of a misunderstanding, I returned the bundle's data in choice
's dehydrate instead of the actual bundle itself.
When tastypie dehydrates choice
, it obviously does not get a bundle object and therefore does not have obj
.