当我做这样的事情时,http://example.com/api/v1/nisit/?format=json。我将得到这个错误“模型‘有一个空属性’页面‘,并且不允许空值。”
我想用味道的反向关系。反转到“页面”模型形式"nisit“模型。我想要的结果是调用127.0.0.1:8000/api/v1/nisit/?format=json&friend_followingPage_id=1
问题的关键是“如何在Nisit模型中设置followingPage属性与页面模型中的followingNisit属性相反
这是我的模型
class Nisit(models.Model):
friends = models.ManyToManyField('self',null=True,blank=True)
class Page(models.Model):
followingNisit = models.ManyToManyField(Nisit,blank=True)这是我的资源
class NisitResource(ModelResource):
friend = fields.ManyToManyField('self','friend',null=True)
followingPage = fields.ToManyField('chula.api.resourse.PageResource','followingPage')
class Meta:
queryset = Nisit.objects.all()
resource_name = 'nisit'
filtering = {
'friend' : ALL_WITH_RELATIONS,
'id' : ALL,
}在上面的代码中。我试着编码>>>> page=.根据这个链接,http://django-tastypie.readthedocs.org/en/latest/resources.html#reverse-relationships,但是它帮不上忙
class PageResource(ModelResource):
followingNisit = fields.ManyToManyField(NisitResource, 'followingNisit',null=True)
class Meta:
queryset = Page.objects.all()
resource_name = 'page'
authorization= Authorization()
filtering = {
'followingNisit': ALL_WITH_RELATIONS,
'p_name': ALL,
}发布于 2013-03-05 04:35:08
试着添加:
class Page(models.Model):
followingNisit = models.ManyToManyField(Nisit,blank=True, related_name="followingPage")清除缓存,并可能删除所有的null=True可能会抑制该错误消息。
https://stackoverflow.com/questions/14658859
复制相似问题