Django模型 - Charfield在另一个模型中连续选择?

时间:2017-11-08 12:15:05

标签: python django

我在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 个答案:

答案 0 :(得分:0)

所以这是一个更好的方法,就像我在评论部分提到的那样:

1 - 您不需要SettingsSettingChoices。它们基本相同,因此您可以将它们组合成一个名为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>

现在你只有你希望它们在表单中显示的选项,即使用户试图弄乱模板代码并添加更多选项,表单也不会允许它们被接受,并且会引发错误。