Tastypie-找不到“嵌套资源”字段 [英] Tastypie - Nested Resource field not found

查看:52
本文介绍了Tastypie-找不到“嵌套资源”字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

#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"} 

已为视频字段提供的数据不是URI,也不是类似
的字典,并且没有'pk '属性:21。

如果我对此行发表评论:

If I comment this line:

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

一切工作正常,但是当我要求 / api / v1 / questions /

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

我的问题是:

也许您的眼睛可以帮助我找到错误:)
谢谢!

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

推荐答案


为视频字段提供的数据不是URI,也不是字典-alike并且没有'pk'属性:21。

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

因此,这意味着整数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.

首先,您可以输入URI作为记录,这可能是最

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"} 

当您注释掉ForeignKey字段时,它起作用的原因是因为当时好吃的将其视为IntegerField

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.

这让我b了一段时间希望对您有所帮助!

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

这篇关于Tastypie-找不到“嵌套资源”字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆