I use Django Generic Relations to define a voting model for question and answer models.
Here is my voting model:
models.py
class Vote(models.Model): user_voted = models.ForeignKey(MyUser) is_upvote = models.BooleanField(default=True)
views.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)
And then I get this error:
FieldError at /1/ Cannot resolve keyword 'content_object' into field. Choices are: answer, content_type, id, is_upvote, object_id, question, user_voted
When I print the โobjectโ in the Django console, it prints the โQuestion 1โ object. Therefore, I do not understand why the string "content_object = object" gives me a field error ...
Any ideas: (((???
thanks
source share