我有以下代码:
class ReportType(models.Model):
REPORT_TYPE_CHOICES = (
('E', 'Earnings'),
('MA', 'Monthly announcement'),
('WA', 'Weekly announcement'),
('SA', 'Sales announcement'),
)
report_type = models.CharField(
max_length=50,
choices=REPORT_TYPE_CHOICES,
default="Earnings"
)
def __str__(self):
return self.report_type
这只是模型类之一,其中包含一个字段的choices属性。但是,在执行“makemigrations”和“migrate”时,管理工具会创建数据库表,但不会在数据库表属性中填充它应该执行的选择中的数据。结果是,当我在modelfrom中使用这个模型时,当单击表单中的下拉框时,会得到一个空的下拉列表。
几乎所有包含choices字段的模型类都会出现此问题,但其中一个模型类实际上正在工作,但除了实际选择中的不同内容外,它具有相同的代码。
有人知道为什么django管理工具没有将choices属性中的数据填充到数据库表中吗?我看不出代码有什么问题。
编辑:
报表类的模型窗体:
class ReportForm(ModelForm):
class Meta:
model = Report
fields = ['profile', 'name', 'report_type', 'time_period', 'link']
reporttype本身没有附加modelform,但它是report类中的一个foreignkey。
报表模型具有以下代码:
class Report(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
report_type = models.ForeignKey(ReportType)
time_period = models.ForeignKey(ReportTimePeriod)
link = models.URLField(max_length=500)
report_conclusion = models.CharField(max_length=500, default="No conclusion yet")
market_reaction = models.CharField(max_length=500, default="No market reaction yet")
reportTimePeriod在其中一个属性中还有一个选项列表,因此我希望它也能在那里填充数据库。