8

我现在每天都在使用 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,我必须使用模型的 color 属性更改每个选项的背景颜色。

你知道我该怎么做吗?

谢谢你。

4

5 回答 5

6

您需要做的是更改由小部件控制的输出。默认是选择小部件,因此您可以对其进行子类化。它看起来像这样:

class Select(Widget):
    def __init__(self, attrs=None, choices=()):
        super(Select, self).__init__(attrs)
        # choices can be any iterable, but we may need to render this widget
        # multiple times. Thus, collapse it into a list so it can be consumed
        # more than once.
        self.choices = list(choices)

    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = ''
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<select%s>' % flatatt(final_attrs)]
        options = self.render_options(choices, [value])
        if options:
            output.append(options)
        output.append('</select>')
        return mark_safe(u'\n'.join(output))

    def render_options(self, choices, selected_choices):
        def render_option(option_value, option_label):
            option_value = force_unicode(option_value)
            selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
            return u'<option value="%s"%s>%s</option>' % (
                escape(option_value), selected_html,
                conditional_escape(force_unicode(option_label)))
        # Normalize to strings.
        selected_choices = set([force_unicode(v) for v in selected_choices])
        output = []
        for option_value, option_label in chain(self.choices, choices):
            if isinstance(option_label, (list, tuple)):
                output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
                for option in option_label:
                    output.append(render_option(*option))
                output.append(u'</optgroup>')
            else:
                output.append(render_option(option_value, option_label))
        return u'\n'.join(output)

这是很多代码。但是您需要做的是使用更改的渲染方法制作您自己的小部件。它是确定创建的 html 的 render 方法。在这种情况下,这是render_options您需要更改的方法。在这里,您可以包括一些检查以确定何时添加您可以设置样式的类。

另一件事,在您上面的代码中,您似乎没有附加最后一组选择。此外,您可能希望向查询集添加一个order_by(),因为您需要按类型对其进行排序。您可以在 init 方法中执行此操作,因此当您使用表单域时不必重新执行此操作。

于 2009-12-10T06:46:25.243 回答
4

render_option已从 Django 1.11 开始删除。这就是我为实现这一目标所做的。一点点挖掘,这看起来简单明了。适用于 Django 2.0+

class CustomSelect(forms.Select):
    def __init__(self, attrs=None, choices=()):
        self.custom_attrs = {}
        super().__init__(attrs, choices)

    def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
        index = str(index) if subindex is None else "%s_%s" % (index, subindex)
        if attrs is None:
            attrs = {}
        option_attrs = self.build_attrs(self.attrs, attrs) if self.option_inherits_attrs else {}
        if selected:
            option_attrs.update(self.checked_attribute)
        if 'id' in option_attrs:
            option_attrs['id'] = self.id_for_label(option_attrs['id'], index)

        # setting the attributes here for the option
        if len(self.custom_attrs) > 0:
            if value in self.custom_attrs:
                custom_attr = self.custom_attrs[value]
                for k, v in custom_attr.items():
                    option_attrs.update({k: v})

        return {
            'name': name,
            'value': value,
            'label': label,
            'selected': selected,
            'index': index,
            'attrs': option_attrs,
            'type': self.input_type,
            'template_name': self.option_template_name,
        }


class MyModelChoiceField(ModelChoiceField):

    # custom method to label the option field
    def label_from_instance(self, obj):
        # since the object is accessible here you can set the extra attributes
        if hasattr(obj, 'type'):
            self.widget.custom_attrs.update({obj.pk: {'type': obj.type}})
        return obj.get_display_name()

表格:

class BookingForm(forms.ModelForm):

    customer = MyModelChoiceField(required=True,
                                  queryset=Customer.objects.filter(is_active=True).order_by('name'),
                                  widget=CustomSelect(attrs={'class': 'chosen-select'}))

我需要的输出如下:

  <select name="customer" class="chosen-select" required="" id="id_customer">
      <option value="" selected="">---------</option>
      <option value="242" type="CNT">AEC Transcolutions Private Limited</option>
      <option value="243" type="CNT">BBC FREIGHT CARRIER</option>
      <option value="244" type="CNT">Blue Dart Express Limited</option>
于 2018-03-29T08:32:32.783 回答
0

我在搜索时多次遇到这个问题

'如何自定义/填充 Django SelectField 选项'

Dimitris Kougioumtzis提供的答案很简单

希望它可以帮助像我这样的人。

# forms.py
from django.forms import ModelForm, ChoiceField
from .models import MyChoices

class ProjectForm(ModelForm):
    choice = ChoiceField(choices=[
        (choice.pk, choice) for choice in MyChoices.objects.all()])
# admin.py
class ProjectAdmin(BaseAdmin):
    form = ProjectForm
    ....
于 2019-05-06T23:37:10.383 回答
-1

http://code.djangoproject.com/browser/django/trunk/django/newforms/widgets.py?rev=7083

类 Select(Widget):中可以看出,无法将样式属性添加到选项标签。为此,您必须继承此小部件并添加此类功能。

Select(Widget):定义仅将样式属性添加到主选择标记。

于 2011-03-29T17:54:02.863 回答
-1

您不应该为向呈现的 html 标记添加一些自定义属性而弄乱表单字段。但是您应该将这些子类化并添加到小部件中。

来自文档:customizing-widget-instances

您可以将attrs字典提交到表单小部件,这些小部件在输出表单小部件上呈现为属性。

class CommentForm(forms.Form):
    name = forms.CharField(
                widget=forms.TextInput(attrs={'class':'special'}))
    url = forms.URLField()
    comment = forms.CharField(
               widget=forms.TextInput(attrs={'size':'40'}))
Django will then include the extra attributes in the rendered output:

>>> f = CommentForm(auto_id=False)
>>> f.as_table()
<tr><th>Name:</th><td><input type="text" name="name" class="special"/></td></tr>
<tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
<tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr>
于 2009-12-10T08:56:29.543 回答