【发布时间】:2015-03-27 13:10:41
【问题描述】:
我的 django 应用程序中有两个模型,其中一个模型到另一个模型中有 ManyToMany 字段。我想为 ManyToMany 字段设置默认值,但无法做到这一点。
我的模型是
Class Model1(models.Model):
name = models.CharField(max_length=100)
class Model2(models.Model):
model1 = models.ManyToManyField(Model1, null=True, blank=True, default=Model1.objects.first())
但是使用这个我得到这个错误
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
我通过定义一个显式变量来尝试它,也像
m1 = Model1.objects.first()
and assigning this m1 variable to the field as default but same error again.
请建议我如何为 django 中的 M2Mfield 分配默认值。我希望当模型表单在模板上呈现时应该选择选项的第一个对象。
【问题讨论】:
标签: python django django-models django-forms