Django表单中,自定义SelectField和SelectMultipleField
我现在每天都使用 Django,已经三个月了,它真的很棒。快速 Web 应用程序开发。
我还有一件事不能完全按照自己的意愿去做。 它是 SelectField 和 SelectMultiple 字段。
我希望能够将一些参数添加到 Select 的选项中。
我终于成功使用了 optgroup :
class EquipmentField(forms.ModelChoiceField):
def __init__(self, queryset, **kwargs):
super(forms.ModelChoiceField, self).__init__(**kwargs)
self.queryset = queryset
self.to_field_name=None
group = None
list = []
self.choices = []
for equipment in queryset:
if not group:
group = equipment.type
if group != equipment.type:
self.choices.append((group.name, list))
group = equipment.type
list = []
else:
list.append((equipment.id, equipment.name))
但是对于另一个 ModelForm,我必须使用模型的颜色属性来更改每个选项的背景颜色。
你知道我该怎么做吗?
谢谢。
I am using Django everyday now for three month and it is really great. Fast web application development.
I have still one thing that I cannot do exactly how I want to.
It is the SelectField and SelectMultiple Field.
I want to be able to put some args to an option of a Select.
I finally success with the optgroup :
class EquipmentField(forms.ModelChoiceField):
def __init__(self, queryset, **kwargs):
super(forms.ModelChoiceField, self).__init__(**kwargs)
self.queryset = queryset
self.to_field_name=None
group = None
list = []
self.choices = []
for equipment in queryset:
if not group:
group = equipment.type
if group != equipment.type:
self.choices.append((group.name, list))
group = equipment.type
list = []
else:
list.append((equipment.id, equipment.name))
But for another ModelForm, I have to change the background color of every option, using the color property of the model.
Do you know how I can do that ?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要做的是更改由小部件控制的输出。默认是选择小部件,因此您可以对其进行子类化。它看起来像这样:
有很多代码。但您需要做的是使用更改的渲染方法来制作您自己的小部件。渲染方法决定了创建的 html。在本例中,您需要更改的是
render_options
方法。在这里,您可以进行一些检查以确定何时添加您可以设置样式的类。另一件事,在上面的代码中,您看起来并没有附加最后的组选择。此外,您可能希望将
order_by()
添加到查询集中,因为您需要按类型对其进行排序。您可以在 init 方法中执行此操作,因此在使用表单字段时不必重新执行此操作。What you need to do, is to change the output which is controlled by the widget. Default is the select widget, so you can subclass it. It looks like this:
It's a lot of code. But what you need to do, is to make your own widget with an altered render method. It's the render method that determines the html that is created. In this case, it's the
render_options
method you need to change. Here you could include some check to determine when to add a class, which you could style.Another thing, in your code above it doesn't look like you append the last group choices. Also you might want to add an
order_by()
to the queryset, as you need it to be ordered by the type. You could do that in the init method, so you don't have to do it all over when you use the form field.render_option
已从 Django 1.11 开始删除。这就是我为实现这一目标所做的事情。稍微挖掘一下,这看起来就简单明了。适用于 Django 2.0+形式:
我需要的输出如下:
render_option
has been removed from Django 1.11 onwards. This is what I did to achieve this. A little bit of digging and this seems straightforward and neat. Works with Django 2.0+The form:
The output which I needed is as:
进行搜索时,我多次遇到这个问题
在通过“如何自定义/填充 Django SelectField 选项”
Dimitris Kougioumtzis 提供的答案很简单
希望它可以帮助像我这样的人。
I run into this question many times when searching by
'how to customize/populate Django SelectField options'
The answer provided by Dimitris Kougioumtzis is quite easy
Hope it could help somebody like me.
您不应该为了向呈现的 html 标记添加一些自定义属性而弄乱表单字段。但是您应该子类化并将这些添加到小部件中。
来自 文档:customizing-widget-instances
您可以将
attrs
字典提交到表单小部件,该字典在输出表单小部件上呈现为属性。You should not mess with form fields for adding some custom attributes to the rendered html tag. But you should subclass and add a these to the Widget.
From the docs: customizing-widget-instances
You can submit
attrs
dictionary to the form Widgets, that render as attributes on the output form widgets.http://code.djangoproject.com/browser /django/trunk/django/newforms/widgets.py?rev=7083
正如在 类 Select(Widget): 下所见,无法添加 样式< /strong> 属性到 option 标记。为此,您必须对该小部件进行子类化并添加此类功能。
class Select(Widget): 定义仅将 style 属性添加到主 select 标记中。
http://code.djangoproject.com/browser/django/trunk/django/newforms/widgets.py?rev=7083
As seen under the class Select(Widget):, there is no way to add the style attribute to an option tag. To this, you will have to subclass this widget and add such functionality.
The class Select(Widget): definition only adds style attribute to the main select tag.