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

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

问题描述

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

  form = MyModel(instance = model_instance)

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



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



编辑:根据要求,我的模型和形式:

 类App(models.Model):
CODES =(
(u'a' ,u'annual'),
(u'm',u'monthly'),
(u'w',u'weekly')

code = models $ char $($)
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 '年度'),
(u'm',u'monthly'),
(u'w',u'weekly')

TIMES =(
(u'00:00',u'All Day'),
(u'12:00',u'Noon')

start_time = forms.ChoiceField = False,choices = TIMES)
end_time =表单.ChoiceField(required = False,choices = TIMES)
code = forms.ChoiceField(choices = CODES,label ='Type')

class Meta:
model = App

有趣的是,代码字段具有模型实例值以HTML格式呈现时,预先选择的格式很好。我不知道在模型定义中是否有选项参数在这里有所区别?



更新: / strong>我刚刚注意到,如果我在 python manage.py shell 中提起了一个 App 实例,就像这样:

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

我得到像$ code> datetime.time(12,0)的值。但是在Django管理员中,当我查看所有 App 实例时,它们都显示(无) start_time end_time 下。为什么会这样?

解决方案

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



当加载模型时,当然,从对象加载的初始值与字段的(models.TimeField)类型。



如果将您的时间替换为

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

您的烦恼应该结束。



Alan


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)

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.

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?

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?

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

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?

解决方案

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

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

If you replace your TIMES with

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

your troubles should be over.

Alan

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

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