Tastypie - Nested Resource field not found

空扰寡人 提交于 2019-11-30 04:18:02

问题


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!


回答1:


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!



来源:https://stackoverflow.com/questions/10242494/tastypie-nested-resource-field-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!