我有这样的代码:
#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
问题是发送该对象时,我得到这个错误:
{"video":21,"text":"sadasds"}
“视频”领域已经被赋予这不是一个URI,而不是一个字典一样,没有一个“pk”文件的属性数据:21。
如果我评论这一行:
video = fields.ForeignKey(VideoResource,'video',full=True)
一切工作正常,但我不能得到这个信息(视频)问到时/api/v1/questions/
我的问题是:
- 我应该创建资源,一个发布和其他检索信息< - 这似乎不是一个很好的解决方案。 要么
- 如何创建嵌套的资源? 我试图按照网上的例子http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources但你可以看到由于某种原因不能正常工作。
也许你的眼睛可以帮我找到错误:)谢谢!