I have this two following classes in my model:
class Answer(models.Model):
answer = models.CharField(max_length=300)
question = models.ForeignKey('Question', on_delete=models.CASCADE)
def __str__(self):
return "{0}, view: {1}".format(self.answer, self.answer_number)
class Vote(models.Model):
answer = models.OneToOneField(Answer, related_name="votes", on_delete=models.CASCADE)
users = models.ManyToManyField(User)
def __str__(self):
return str(self.answer.answer)[:30]
In the shell I take the first Answer:
>>> Answer.objects.all()[0]
<Answer: choix 1 , view: 0>
I want to get the Vote object:
>>> Answer.objects.all()[0].votes
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\Hippolyte\AppData\Roaming\Python\Python38\site-packages\django\db\models\fields\related_descriptors.py", line 420, in __get__
raise self.RelatedObjectDoesNotExist(
questions.models.Answer.votes.RelatedObjectDoesNotExist: Answer has no votes.
But an error occured.
I don't understand why the related_name is not recognized. Could you help me ?