1

我有一个带有以下选择列表的模型:

models.py

class Choice_class(models.Model):

    options = (
        ('None', 'none'),
        ('one', 'one'),
        ('two', 'two'),
        ('three', 'three'),
    )

    option_values = (
        ('None', 0),
        ('one', 1),
        ('two', 2),
        ('three', 3),
    )

    user_choice = models.CharField(max_length = 5, choices = options, default = 'None')
    choice_value = models.IntegerField()

和如下表格: forms.py

class Choice_class(ModelForm):

    options = (
        ('None', 'none'),
        ('one', 'one'),
        ('two', 'two'),
        ('three', 'three'),
    )

    option_values = (
        ('None', 0),
        ('one', 1),
        ('two', 2),
        ('three', 3),
    )

    user_choice = models.CharField(max_length = 5, choices = options, default = 'None')
    choice_value = models.IntegerField(default = option_values[#BASED ON USER CHOICE IN FIELD ABOVE] )

我想根据用户为该字段选择的内容使用choice_value默认值填充该字段。option_valuesuser_choice

我宁愿不通过 js 来做到这一点(尽管它是一个选项),因为我觉得应该有一种方法可以通过框架以相同的形式做到这一点。

我意识到我也可以选择为此执行逻辑views.py

if choice_value == 'None': 
   '''populate with default value based on user_choice as defined by dict in views file etc'''

但如果可能的话,我宁愿在表格中这样做。

如果您有任何指示,请提前致谢。

4

0 回答 0