Django ModelForm ChoiceField不显示实例数据

时间:2022-07-08 19:20:52

I have a ModelForm class in which I set a couple of the fields as ChoiceField. For one of my views, I'd like to create a form from my ModelForm class that pulls from an instance of my model in the database (like so):

我有一个ModelForm类,我在其中设置了几个字段作为ChoiceField。对于我的一个观点,我想从我的ModelForm类中创建一个从数据库中的模型实例中提取的表单(如下所示):

form = MyModel(instance=model_instance)

When I do this and then render the form in a template, I've noticed that most of the fields are pre-populated with values pulled from the model instance, which is what I want. However, this isn't the case for two ChoiceField fields. These render as drop-down select menus with no specific option selected.

当我这样做然后在模板中渲染表单时,我注意到大多数字段都是预先填充了从模型实例中提取的值,这正是我想要的。但是,两个ChoiceField字段不是这种情况。这些呈现为下拉选择菜单,未选择任何特定选项。

What's strange is if I don't define those two fields as ChoiceField-type in my ModelForm class, they render as normal text input fields in HTML and pre-populate using the database values. But when I define them so they show up as select-option input fields in HTML, nothing is pre-selected. Can I change this so that the values from the database are pre-selected?

奇怪的是,如果我在ModelForm类中没有将这两个字段定义为ChoiceField类型,它们将在HTML中呈现为普通文本输入字段,并使用数据库值进行预填充。但是当我定义它们以便它们在HTML中显示为选择选项输入字段时,没有预先选择任何内容。我可以更改此项以便预先选择数据库中的值吗?

EDIT: As requested here is the code for my model and form:

编辑:这里要求的是我的模型和表格的代码:

class App(models.Model):
    CODES = (
        (u'a',u'annual'),
        (u'm',u'monthly'),
        (u'w',u'weekly')
    )
    code = models.CharField(max_length=1, choices=CODES)
    start_time = models.TimeField(blank=True, null=True)
    end_time = models.TimeField(blank=True, null=True)


class AppForm(ModelForm):
    CODES = (
        (u'',u'Please select code'),
        (u'a',u'annual'),
        (u'm',u'monthly'),
        (u'w',u'weekly')
    )
    TIMES = (
        (u'00:00',u'All Day'),
        (u'12:00',u'Noon')
    )
    start_time = forms.ChoiceField(required=False, choices=TIMES)
    end_time = forms.ChoiceField(required=False, choices=TIMES)
    code = forms.ChoiceField(choices=CODES, label='Type')

    class Meta:
        model = App

Interestingly, code field has the model instance value preselected just fine when rendered as HTML. I wonder if having the choices argument in the model definition makes the difference here?

有趣的是,代码字段在呈现为HTML时预先选择了模型实例值。我想知道在模型定义中有选择参数是否会产生差异?

UPDATE: I just noticed that if I pull up an App instance in the python manage.py shell like so:

更新:我刚注意到,如果我在python manage.py shell中提取一个App实例,如下所示:

a = App.objects.get(id=16)
a.start_time

I get a value like datetime.time(12, 0). But in the Django admin, when I'm looking at all of the App instances, all of them show (None) under start_time and end_time. Why would that be?

我得到一个像datetime.time(12,0)的值。但是在Django管理员中,当我查看所有App实例时,所有这些实例都在start_time和end_time下显示(None)。那为什么会这样?

1 个解决方案

#1


1  

In response to your update : your times strings match default time string HH:MM format. Just like a user would enter them from website manually 12:00. The values get parsed and turned into time at model save (at validating really).

响应您的更新:您的时间字符串匹配默认时间字符串HH:MM格式。就像用户手动从网站输入12:00一样。这些值在模型保存时解析并转换为时间(真正验证时)。

And when you load model - then of course the initial values loaded from object match the field's (models.TimeField) type.

当你加载模型时 - 当然从对象加载的初始值匹配字段的(models.TimeField)类型。

If you replace your TIMES with

如果你用你的TIMES替换

    (datetime.time(0,0),u'All Day'),
    (datetime.time(12,0),u'Noon')

your troubles should be over.

你的烦恼应该结束了。

Alan

#1


1  

In response to your update : your times strings match default time string HH:MM format. Just like a user would enter them from website manually 12:00. The values get parsed and turned into time at model save (at validating really).

响应您的更新:您的时间字符串匹配默认时间字符串HH:MM格式。就像用户手动从网站输入12:00一样。这些值在模型保存时解析并转换为时间(真正验证时)。

And when you load model - then of course the initial values loaded from object match the field's (models.TimeField) type.

当你加载模型时 - 当然从对象加载的初始值匹配字段的(models.TimeField)类型。

If you replace your TIMES with

如果你用你的TIMES替换

    (datetime.time(0,0),u'All Day'),
    (datetime.time(12,0),u'Noon')

your troubles should be over.

你的烦恼应该结束了。

Alan