[英]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.