Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django+backbone+tastypie: handling relationship [duplicate]

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:

  • Should I create to resources, one to post and another to retrieve information <- this seems not a really good solution. or
  • How can I create Nested Resources ? I tried to follow the example on the web http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources but as you can see for some reason is not working.

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

like image 927
Mc- Avatar asked Apr 20 '12 08:04

Mc-


1 Answers

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!

like image 140
krs Avatar answered Sep 28 '22 05:09

krs