how to add correspondence between two models
for example, the following two classes:
from django.db import models
class Question(models.Model):
class Meta:
verbose_name = ""
verbose_name_plural = ""
question_text = models.CharField("",max_length = 100)
pub_date = models.DateTimeField("")
def __str__(self):
return self.question_text
class Choice(models.Model):
class Meta:
verbose_name = ""
verbose_name_plural = ""
def __str__(self):
return self.choice_text
question = models.ForeignKey(Question, on_delete = models.CASCADE)
choice_text = models.CharField("",max_length = 50)
votes = models.IntegerField("",default = 0)
one-to-one relationship to Question
is defined in Choice
.
according to the train of thought, one-to-many relationship to Choice
should be added to Question
, but if it is added in Question
, because the Choice
class has not been defined yet, it will report an error
.