简体   繁体   English

Tastypie-找不到“嵌套资源”字段

[英]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. 已为“视频”字段提供的数据不是URI,也不是类似字典的数据,并且没有“ pk”属性: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/ 一切正常,但是当我询问/api/v1/questions/时,我无法获得此信息(视频)

My question is: 我的问题是:

maybe your eyes can help me find the error :) Thanks! 也许您的眼睛可以帮助我发现错误:)谢谢!

The 'video' field has was given data that was not a URI, not a dictionary-alike and does not have a 'pk' attribute: 21. 已为“视频”字段提供的数据不是URI,也不是类似字典的数据,并且没有“ pk”属性: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. 因此,这意味着整数21不满足该字段的要求,也模糊地暗示了将满足要求的条件。

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. 首先,您可以发送记录的URI,这可能是最正确的方法,因为URI确实是唯一的,而pk却不是。

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

or, you can send in an dictionary-alike object with the pk field set. 或者,您可以发送带有pk字段集的类似字典的对象。

{"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. 当您注释掉ForeignKey字段时,它起作用的原因是因为当时好吃的将其视为IntegerField,可以由一个普通整数引用。

This had me stunted for a while to, hope it helps! 这让我unt了一段时间,希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM