Django の Generic Relations を使用して、Q&A モデルの Vote モデルを定義しています。
これが私の投票モデルです:
models.py
class Vote(models.Model):
user_voted = models.ForeignKey(MyUser)
is_upvote = models.BooleanField(default=True)
# Generic foreign key
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class Meta:
unique_together = ('content_type', 'user_voted')
ビュー.py
user_voted = MyUser.objects.get(id=request.user.id)
object_type = request.POST.get('object_type')
object = None;
if object_type == 'question':
object = get_object_or_404(Question, id=self.kwargs['pk'])
elif object_type == 'answer':
object = get_object_or_404(Answer, id=self.kwargs['pk'])
# THIS LAST LINE GIVES ME THE ERROR
vote, created = Vote.objects.get_or_create(user_voted=user_voted, content_object=object)
そして、私はこのエラーを受け取ります:
FieldError at /1/
Cannot resolve keyword 'content_object' into field. Choices are: answer, content_type, id, is_upvote, object_id, question, user_voted
「オブジェクト」を Django コンソールに出力すると、「質問 1」オブジェクトが出力されます。したがって、「content_object=object」という行でフィールドエラーが発生する理由がわかりません...
何か案は :(((???
ありがとう