我正在开发一个问答应用程序,我正在使用 django-tastypie 和 jquery-ajax 创建资源。我有两个模型或资源,问题和答案。问题具有 TopicResouces 和 DifficultyLevelResource 的外键。并且 Answer 有问题的外键。当我一一创建资源时,一切正常。但现在我正在尝试创建相关对象以及 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 组的问题,但无法理解问题。告诉我我在做什么是正确的做法。帮助将不胜感激