我最近开始使用 django-tastypie 并且到目前为止很喜欢这个框架。话虽如此,我在 POST 上遇到了 OneToOne 与模型的关系的问题,并且花了很多时间但不知道缺少什么。这是模型和资源代码 -
模型
class Question(TimeStampedModel):
question_title = models.CharField("question title", max_length=100)
question_desc = models.TextField("question description", max_length=1000)
.......
第二种模型与问题具有 OneToOne 关系 -
class QuestionAnswer(TimeStampedModel):
question = models.OneToOneField(Question)
.....
资源
QuestionAnswerResource -
class QuestionAnswerResource(ModelResource):
question = fields.ForeignKey('myapp.api.QuestionResource', 'question')
class Meta:
queryset = QuestionAnswer.objects.all()
resource_name='questionanswer'
问题资源 -
class QuestionResource(ModelResource):
questionanswer = fields.OneToOneField('myapp.api.QuestionAnswerResource', 'questionanswer', full=True)
class Meta:
queryset = Question.objects.all()
resource_name = 'question'
通过上述设置,我在 GET 上得到了带有 Question 实例及其 answer 属性的正确响应。但是,当我尝试发布数据以保存问题/答案时,它会失败并出现以下错误 -
"{"error_message": "", "traceback": "Traceback (最近一次调用最后一次):\n\n 文件\"/Library/Python/2.7/site-packages/tastypie/resources.py\", 第192行, .................................................. ............................. 第 636 行,水合物\n
值 = super(ToOneField, self).hydrate(bundle)\n\n 文件 \"/Library/Python/2.7/site-packages/tastypie/fields.py\",第 154 行,在 hydrate\n elif self.attribute 和 getattr(bundle.obj , self.attribute, None):\n\n 文件\"/Library/Python/2.7/site-packages/Django-1.4.1-py2.7.egg/django/db/models/fields/related.py\ ",第 343 行,在获取\n raise self.field.rel.to.DoesNotExist\n\nDoesNotExist\n"}"
有人可以指出我缺少什么吗?