Mc-
Mc-

Reputation: 4056

Tastypie - Nested Resource field not found

I have this code:

#api model 

class VideoResource(ModelResource):
    class Meta:
        queryset = Video.objects.all()
        include_resource_uri = False
        resource_name = 'video'
        authorization = DjangoAuthorization()

class QuestionResource(ModelResource):

    user = fields.ToOneField(UserResource,'user',full=True)
    video = fields.ForeignKey(VideoResource,'video',full=True)

    class Meta:
        queryset = Question.objects.all()
        resource_name = 'question'
        include_resource_uri = False
        authorization = DjangoAuthorization()

    def obj_create(self, bundle, request=None, **kwargs):
        import json
        temp = json.loads(request.body, object_hook=_decode_dict)
        video = Video.objects.get(pk=temp['video'])
        return super(QuestionResource, self).obj_create(bundle, request, user=request.user, video=video)

#model

class Question(models.Model):
    text = models.CharField('Question',max_length=120)
    created = models.DateTimeField(auto_now_add=True)
    enabled = models.BooleanField(default=True)
    flag = models.BooleanField(default=False)
    allow_comments = models.BooleanField(default=True)
    thumbnail_url = models.CharField(default='video.jpg',blank=True, null=True,max_length=200)

    user = models.ForeignKey(User)
    video = models.ForeignKey(Video)

    def __unicode__(self): 
        return self.text;

class Video(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now_add=True)
    url = models.URLField(default="")

    user = models.ForeignKey(User)

    def __unicode__(self): 
        return str(self.pk) + ' > ' + self.status

The problem is that I am getting this error when sending this object:

{"video":21,"text":"sadasds"} 

The 'video' field has was given data that was not a URI, not a dictionary-alike and does not have a 'pk' attribute: 21.

If I comment this line:

video = fields.ForeignKey(VideoResource,'video',full=True) 

Everything works fine, but then I cannot get this information (video) when asking to /api/v1/questions/

My question is:

maybe your eyes can help me find the error :) Thanks!

Upvotes: 7

Views: 2438

Answers (1)

krs
krs

Reputation: 4154

The 'video' field has was given data that was not a URI, not a dictionary-alike and does not have a 'pk' attribute: 21.

So, this means that the integer 21 does't meet the requirements for that field, it also give a vague hint of what will meet the requirements.

first, you can send in the URI for the record, this is probably the most correct way as URIs are really unique while pk's are not.

{"video":"/api/v1/video/21","text":"sadasds"} 

or, you can send in an dictionary-alike object with the pk field set.

{"video":{'pk':21},"text":"sadasds"} 

The reason it works when you comment out the ForeignKey field is because then tastypie treats it as a IntegerField, which can be referenced by a plain integer.

This had me stunted for a while to, hope it helps!

Upvotes: 6

Related Questions