我正在开发一个Q& A应用程序,我正在使用django-tastypie和jquery-ajax来创建资源。我有两个模型或资源,问题和答案。问题有TopicResouces和DifficultyLevelResource的外键。和答案有外国问题。当我逐个创建资源时,一切正常。但现在我正在尝试创建相关对象以及django-Tastypie中描述的对象。我的资源代码是
class QuestionResource(ModelResource):
topic = fields.ToOneField('courses.api.TopicResource','topic',null=True)
difficulty_level = fields.ForeignKey(DifficultyLevelResource, 'difficulty_level',null=True,full=True)
answers = fields.ToManyField('quiz.api.AnswerResource', 'answer_set', null=True, full=True)
class Meta:
queryset = Question.objects.all()
class AnswerResource(ModelResource):
question=fields.ForeignKey(QuestionResource,'question',null=True)
class Meta(CommonMeta):
queryset = Answer.objects.all()
resource_name = 'answer'
我发布的内容是
curl -v -H "Content-Type: application/json" -X POST --data '{"explanation":"1+1+1+1=4","hint":"asadsa","question_text":"2+2","topic":"/api/v1/topic/2/","question_type": "NUM","difficulty_level":"/api/v1/difficultylevel/1/", "answers":[{"answer_text": "8","marks": "1.00"}]}' http://serverpath /api/vi/question/
但它总是给我404错误。
当我从浏览器发送请求时
{"topic":"path to the related topic object ","question_text":"2+2","difficulty_level":"path to fficultylevel","question_type":"MCQ","explanation":"sssa","hint":"ssss","answers":[{"answer_text":"0","marks":"-0.25"},{"answer_text":"2","marks":"0.00"},{"answer_text":"3","marks":"-0.33"},{"answer_text":"None","marks":"1.00"}]}
它给我追溯错误
{"error_message": "", "traceback": "Traceback (most recent call last):\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/resources.py\",
line 202, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/resources.py\",
line 439, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/resources.py\",
line 471, in dispatch\n response = method(request, **kwargs)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/resources.py\",
line 1313, in post_list\n updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/resources.py\",
line 2079, in obj_create\n return self.save(bundle)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2230,
in save\n m2m_bundle = self.hydrate_m2m(bundle)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/resources.py\", line 930, in hydrate_m2m\n
bundle.data[field_name] = field_object.hydrate_m2m(bundle)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/fields.py\",
line 853, in hydrate_m2m\n m2m_hydrated.append(self.build_related_resource(value, **kwargs))\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/fields.py\",
line 661, in build_related_resource\n return self.resource_from_data(self.fk_resource, value, **kwargs)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/fields.py\",
line 620, in resource_from_data\n return fk_resource.full_hydrate(fk_bundle)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/resources.py\", line 881, in full_hydrate\n
value = field_object.hydrate(bundle)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/fields.py\", line 732, in hydrate\n value = super(ToOneField, self).hydrate(bundle)\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/tastypie/fields.py\",
line 165, in hydrate\n elif self.attribute and getattr(bundle.obj, self.attribute, None):\n\n File \"/home/learnomatics/hark-v0.1/local/lib/python2.7/site-packages/django/db/models/fields/related.py\", line 389, in __get__\n
raise self.field.rel.to.DoesNotExist\n\nDoesNotExist\n"
}
我在stackoverflow和Google群组上有很多问题,但无法理解问题。告诉我我正在做的是正确的做法。帮助将不胜感激
答案 0 :(得分:3)
我得到了解决方案。答案在这里:
RelatedField.related_name
用于帮助在创建数据时自动填充反向关系。默认为无。
为了使此选项正常工作,其他资源上必须有一个字段,并将其作为属性/ instance_name。通常这只是意味着添加一个反射的ToOneField指向后面。
示例:
class EntryResource(ModelResource):
authors = fields.ToManyField('path.to.api.resources.AuthorResource', 'author_set', related_name='entry')
class Meta:
queryset = Entry.objects.all()
resource_name = 'entry'
class AuthorResource(ModelResource):
entry = fields.ToOneField(EntryResource, 'entry')
class Meta:
queryset = Author.objects.all()
resource_name = 'author'
使用related_name完成任务。它映射相关字段的对象,并在创建数据时自动填充关系。