我正在尝试使用Tastypie在API中关联两个资源(模型)但我收到错误。
我已关注django tutorial并使用了:
models.py
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
我尝试根据此stackoverflow answer在Poll和Choice之间创建一个链接,并编写了以下代码:
api.py
class ChoiceResource(ModelResource):
poll = fields.ToOneField('contact.api.PollResource', attribute='poll', related_name='choice')
class Meta:
queryset = Choice.objects.all()
resource_name = 'choice'
class PollResource(ModelResource):
choice = fields.ToOneField(ChoiceResource, 'choice', related_name='poll', full=True)
class Meta:
queryset = Poll.objects.all()
resource_name = 'poll'
当我去:127.0.0.1:8088 / contact / api / v1 / choice /?format = json
一切都按预期运作。例如,我的一个选择链接到正确的民意调查:
{
"choice_text": "Nothing",
"id": 1,
"poll": "/contact/api/v1/poll/1/",
"resource_uri": "/contact/api/v1/choice/1/",
"votes": 6
}
当我去:127.0.0.1:8088 / contact / api / v1 / poll /?format = json
我明白了:
{
"error": "The model '<Poll: What's up?>' has an empty attribute 'choice' and doesn't allow a null value."
}
我是否需要使用fields.ToManyField,还是需要更改原始模型?
答案 0 :(得分:0)
Tastypie recommends against creating reverse relationships(您在此处尝试的关系是Choice
- &gt; Poll
,您希望Poll
- &gt; Choice
) ,但如果你还想,你可以。
摘自Tastypie文档:
与Django的ORM不同,Tastypie不会自动创建反向 关系。这是因为存在大量技术复杂性 涉及,以及可能无意中暴露相关数据 对API最终用户的错误方式。
但是,仍然可以创建反向关系。代替 将ToOneField或ToManyField交给一个类,将它们传递给它们 表示所需类的完整路径。实施反向 关系看起来像这样:
# myapp/api/resources.py
from tastypie import fields
from tastypie.resources import ModelResource
from myapp.models import Note, Comment
class NoteResource(ModelResource):
comments = fields.ToManyField('myapp.api.resources.CommentResource', 'comments')
class Meta:
queryset = Note.objects.all()
class CommentResource(ModelResource):
note = fields.ToOneField(NoteResource, 'notes')
class Meta:
queryset = Comment.objects.all()