如何从应用程序外部导入变量列表作为字符字段的选择?
How do i import list of variables as choices for a charfield from outside the app?
我正在尝试在管理中创建一个 select 框,显示当前应用程序之外的数据库中的对象列表。这是我的模型
from typefaces.models import Typeface
class Word(models.Model):
text = models.CharField(max_length=200)
family_select = models.CharField(max_length=100, choices=Typeface.objects.all)
不幸的是,Django 告诉我 'choices' must be an iterable (e.g., a list or tuple).
但是我尝试使用 iter()
使其可迭代但没有成功。
这是完全错误的做法。模型之间的关系应使用 OneToOneFields、ForeignKeys(one-to-many 字段)和 ManyToManyFields 指定。您应该将 CharField 更改为:
family = models.ForeginKey(Typeface, related_name='words')
如果您有特定原因不使用普遍可接受的方式,请进一步详细说明以获得答案。
我正在尝试在管理中创建一个 select 框,显示当前应用程序之外的数据库中的对象列表。这是我的模型
from typefaces.models import Typeface
class Word(models.Model):
text = models.CharField(max_length=200)
family_select = models.CharField(max_length=100, choices=Typeface.objects.all)
不幸的是,Django 告诉我 'choices' must be an iterable (e.g., a list or tuple).
但是我尝试使用 iter()
使其可迭代但没有成功。
这是完全错误的做法。模型之间的关系应使用 OneToOneFields、ForeignKeys(one-to-many 字段)和 ManyToManyFields 指定。您应该将 CharField 更改为:
family = models.ForeginKey(Typeface, related_name='words')
如果您有特定原因不使用普遍可接受的方式,请进一步详细说明以获得答案。