python Django ModelForm CheckBox 小部件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1268209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:48:59  来源:igfitidea点击:

Django ModelForm CheckBox Widget

pythondjangodjango-modelsdjango-forms

提问by f4nt

I'm currently have an issue, and likely overlooking something very trivial. I have a field in my model that should allow for multiple choices via a checkbox form (it doesn't have to be a checkbox in the admin screen, just in the form area that the end-user will see). Currently I have the field setup like so:

我目前有一个问题,可能忽略了一些非常微不足道的事情。我的模型中有一个字段,它应该允许通过复选框表单进行多项选择(它不必是管理屏幕中的复选框,只需在最终用户将看到的表单区域中)。目前我的字段设置如下:

# Type of Media
MEDIA_CHOICES = (
    ('1', 'Magazine'),
    ('2', 'Radio Station'),
    ('3', 'Journal'),
    ('4', 'TV Station'),
    ('5', 'Newspaper'),
    ('6', 'Website'),
)
media_choice = models.CharField(max_length=25,
    choices=MEDIA_CHOICES)

I need to take that and make a checkbox selectable field in a form out of it though. When I create a ModelForm, it wants to do a drop down box. So I naturally overrode that field, and I get my checkbox that I want. However, when the form's submitted, it would appear that nothing useful is saved when I look at the admin screen. The database does however show that I have a number of things selected, which is a positive sign. However, how can I get that to reflect in the admin screen properly?

不过,我需要接受它并在表单中创建一个复选框可选字段。当我创建一个 ModelForm 时,它想做一个下拉框。所以我自然而然地覆盖了那个字段,我得到了我想要的复选框。但是,当表单提交时,当我查看管理屏幕时,似乎没有保存任何有用的内容。然而,数据库确实显示我选择了许多东西,这是一个积极的信号。但是,我怎样才能让它正确地反映在管理屏幕中?

Edit: FWIW I'll gladly accept documentation links as answers, because it would seem I'm just glossing over something obvious.

编辑:FWIW 我很乐意接受文档链接作为答案,因为看起来我只是在掩盖一些明显的东西。

回答by Benjamin Wohlwend

In such a case, the easiest way is to put the choices into a separate model and then use a ManyToMany relationship. After that, you simply override the ModelForm's widget for that field to use forms.CheckboxSelectMultipleand Django will automatically do the right thing. If you insist to use a CharField, you'll probably have to do something like this snippet.

在这种情况下,最简单的方法是将选项放入单独的模型中,然后使用多对多关系。之后,您只需覆盖该字段的 ModelForm 小部件以使用forms.CheckboxSelectMultiple,Django 将自动执行正确的操作。如果您坚持使用 CharField,则可能必须执行类似此代码段的操作

@ 2. comment: how are you overriding the widget? This is how I do it and it works flawlessly:

@ 2. 评论:你是如何覆盖小部件的?这就是我的工作方式,它完美无缺:

class SomeModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(SomeModelForm, self).__init__(*args, **kwargs)
        self.fields['some_field'].widget = forms.CheckboxSelectMultiple()

回答by monkut

I've just started to look into widgets assignment with ModelForms. In a lot of examples I've seen, piquadrat's included, the Form's __ init __ method is overridden.

我刚刚开始研究 ModelForms 的小部件分配。在我见过的很多例子中,包括 piquadrat,Form 的 __ init __ 方法被覆盖。

I find this a little confusing, and just overriding the desired field is more natural for me:

我觉得这有点令人困惑,只是覆盖所需的字段对我来说更自然:

class SomeModelForm(forms.ModelForm):
    some_field = forms.CharField(choices=MEDIA_CHOICES,
                                 widget=forms.CheckboxSelectMultiple)
    class Meta:
        model=SomeModel

Note: I'm using Django 1.1.

注意:我使用的是 Django 1.1。

回答by andrea

Using piquadrat's answer worked for me, but needed to add a line to define the queryset for the M2M. See this link.

使用 piquadrat 的答案对我有用,但需要添加一行来定义 M2M 的查询集。请参阅此链接