Django ModelForm ChoiceField 不显示实例数据 [英] Django ModelForm ChoiceField not displaying instance data

查看:20
本文介绍了Django ModelForm ChoiceField 不显示实例数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ModelForm 类,我在其中将几个字段设置为 ChoiceField.对于我的一个视图,我想从我的 ModelForm 类中创建一个表单,该类从数据库中的模型实例中提取(像这样):

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):

form = MyModel(instance=model_instance)

当我这样做然后在模板中呈现表单时,我注意到大多数字段都预先填充了从模型实例中提取的值,这正是我想要的.但是,对于两个 ChoiceField 字段,情况并非如此.这些呈现为没有选择特定选项的下拉选择菜单.

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.

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

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?

这里要求的是我的模型和表单的代码:

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

有趣的是,code 字段在呈现为 HTML 时预选了模型实例值.我想知道模型定义中的 choices 参数在这里是否有所不同?

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?

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

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

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

我得到一个类似 datetime.time(12, 0) 的值.但是在 Django 管理员中,当我查看所有 App 实例时,它们都在 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?

推荐答案

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

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).

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

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

如果您将 TIMES 替换为

If you replace your TIMES with

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

你的麻烦应该结束了.

艾伦

这篇关于Django ModelForm ChoiceField 不显示实例数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆