1

リソースを作成するために django-tastypie と jquery-ajax を使用している Q&A アプリケーションに取り組んでいます。質問と回答の 2 つのモデルまたはリソースがあります。質問には TopicResouces と DifficultyLevelResource への外部キーがあります。Answer には question への外部キーがあります。リソースを1つずつ作成しているときは、すべて正常に機能しています。しかし今、私は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 グループに関する多くの質問をチェックアウトしましたが、問題を理解できません。私がやっていることは正しい方法だと教えてください。助けていただければ幸いです

4

1 に答える 1

3

私は解決策を得ました。答えはここにあります:

http://django-tastypie.readthedocs.org/en/v0.10.0/fields.html#tastypie.fields.RelatedField.related_name

RelatedField.related_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 を使用すると、タスクが実行されます。関連するフィールドのオブジェクトをマッピングし、データの作成時に関係を自動的に入力します。

于 2013-11-12T05:18:12.240 に答える