[英]Django Model - Charfield with choices from a row in another model?
我已经按照以下说明在Django中创建了一个设置表:-
class Settings(models.Model):
name = models.CharField(max_length=200)
class Meta:
verbose_name = "Settings"
verbose_name_plural = "Settings"
def __str__(self):
return self.name
class SettingChoices(models.Model):
setting = models.ForeignKey(Settings, on_delete=models.PROTECT)
choice = models.CharField(max_length=200)
class Meta:
verbose_name = "Setting Choices"
verbose_name_plural = "Setting Choices"
def __str__(self):
return '{0} - {1}'.format(self.setting, self.choice)
并将其用作示例:
Setting = Circuit Type:
Choices:
DSL
4G
Fibre
然后在另一个模型中,我希望能够将此作为一组选择进行引用
class Circuits(models.Model):
site_data = models.ForeignKey(SiteData, verbose_name="Site", on_delete=models.PROTECT)
order_no = models.CharField(max_length=200, verbose_name="Order No")
expected_install_date = models.DateField()
install_date = models.DateField(blank=True, null=True)
circuit_type = models.CharField(max_length=100, choices=*** here I would get model settings - Circuit Type - Choices ***)
目前,我在settings.py中使用了一个列表,但它并不流畅,我需要我的用户能够更改这些设置,而不是让我手动在settings.py中编辑列表并每次推送更改
我尝试了以下方法:
functions.py
def settings_circuit_types():
from home.models import SettingChoices
type_data = SettingChoices.objects.filter(setting__name='CIRCUIT_TYPES')
circuit_types = []
for c in type_data:
circuit_types.append(c.choice)
return circuit_types
models.py
from app.functions import settings_circuit_types
CIRCUIT_CHOICES = settings_circuit_types()
...
circuit_type = models.CharField(max_length=100, choices= CIRCUIT_CHOICES)
但这引发了错误
dango.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
这是可以理解的,我想知道通过其他方式能否实现?
谢谢
所以这是一种更好的方法,正如我在评论部分中提到的那样:
1-您不需要Settings
和SettingChoices
。 它们基本相同,因此您可以将它们合并为一个称为Setting
模型:
class Setting(models.Model):
name = models.CharField(max_length=200)
# if you need anything that you might think you need another model for,
# just think this way, add a BooleanField.
# for example if you have a setting available only for admins:
only_admin = models.BooleanField(default=False)
# then when you're going to make a from, just filter the options;
# options = setting.objects.filter(only_admin=False)
class Meta:
verbose_name = "Settings"
verbose_name_plural = "Settings"
def __str__(self):
return self.name
2-对于Circuits
模型,您只需要一个简单的ForeignKey
字段:
class Circuits(models.Model):
site_data = models.ForeignKey(SiteData, verbose_name="Site", on_delete=models.PROTECT)
order_no = models.CharField(max_length=200, verbose_name="Order No")
expected_install_date = models.DateField()
install_date = models.DateField(blank=True, null=True)
circuit_type = models.ForeignKey(Setting, null=False, blank=False)
现在,当您要为用户填写表格时:
forms.py
:
class CircuitsForm(forms.ModelForm):
class Meta:
model = Circuits
fields = ('install_date', 'circuit_type') # or other fields.
# and to filter which choices are available to choose from:
def __init__(self, *args, **kwargs):
super(CircuitsForm, self).__init__(*args, **kwargs)
self.fields["circuit_type"].queryset = setting.objects.filter(only_admin=False)
这样,您就可以安全便捷地为用户和管理员创建表单。
您可以编辑管理面板本身,也可以只为管理员用户使用此类表单创建一个网址。
另外,如果您不是那种使用django呈现表单的人,则可以简单地在视图中获取可用的选项并将其传递给模板,如下所示:
settings = setting.objects.filter(only_admin=False)
并将其呈现在这样的模板中:
<select name="circuit_type">
{% for setting in settings %}
<option value="{{ setting.pk }}">{{ setting.name }}</option>
{% endfor %}
</select>
现在,您只有希望它们以表单显示的选项,即使用户试图弄乱模板代码并添加更多选项,表单也不允许它们被接受,并且会为此引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.