DJANGO:ModelChoiceField optgroup标签

pro*_*san 33 python django django-templates django-forms

如何在ModelChoiceField中设置optgroup标签?

这是一个例子:

models.py

class Link(models.Model):
    config = models.ForeignKey(Config)
    name = models.URLField(u'Name', null=True, max_length=50)
    gateway = models.IPAddressField(u'Gateway', null=True)
    weight = models.IntegerField(u'Weight', null=True)
    description = models.TextField(u'Description', blank=True)

def __unicode__(self):
    return self.name
Run Code Online (Sandbox Code Playgroud)

forms.py

class LinkForm(ModelForm):
    config = ModelChoiceField(queryset=Config.objects.all(), empty_label="Choose a link",widget=GroupedSelect())

class Meta:
    model = Link
Run Code Online (Sandbox Code Playgroud)

我想像我这样渲染我的ChoiceField:

example.html的

<select id="id_config" name="config">
    <option selected="selected" value="">Choose a link</option>
    <optgroup label="Configuration" >
        <option value="8">Address: 192.168.1.202/255.255.255.0 </option>
        <option value="9">Address: 192.168.1.240/255.255.255.0 </option>
        <option value="10">Address: 192.168.3.1/255.255.255.0 </option>
    </optgroup>
</select>
Run Code Online (Sandbox Code Playgroud)

**更新**

我这样解决了我的问题:

class GroupedSelect(Select):
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = ''
        final_attrs = self.build_attrs(attrs, name=name)
        output = [format_html('<select{0}>', flatatt(final_attrs))]
        for index, option_gp in enumerate(self.choices):
            if index == 0:
                option_value = smart_unicode(option_gp[0])
                option_label = smart_unicode(option_gp[1])
                output.append(u'<option value="%s">%s</option>' %  (escape(option_value), escape(option_label)))
                output.append('<optgroup label = "Configuration">')
            elif index!=0 and index <= len(self.choices):
                option_value = smart_unicode(option_gp[0])
                option_label = smart_unicode(option_gp[1])
                output.append(u'<option value="%s">%s</option>' % (escape(option_value), escape(option_label)))          
        output.append(u'</optgroup>')
        output.append(u'</select>')
        return mark_safe('\n'.join(output))
Run Code Online (Sandbox Code Playgroud)

Ste*_*liu 67

你不需要创建任何自定义字段,Django已经完成了这项工作,只需传递格式良好的选项:

MEDIA_CHOICES = (
 ('Audio', (
   ('vinyl', 'Vinyl'),
   ('cd', 'CD'),
  )
 ),
 ('Video', (
   ('vhs', 'VHS Tape'),
   ('dvd', 'DVD'),
  )
 ),
)
Run Code Online (Sandbox Code Playgroud)

  • 但这适用于`ChoiceField`,而不是'ModelChoiceField`对吗? (5认同)

Ben*_*hon 8

ModelChoiceField使用 aModelChoiceIterator将查询集转换为选项列表。您可以轻松地覆盖此类来引入组。以下是按国家/地区对城市进行分组的示例:

from itertools import groupby
from django.forms.models import ModelChoiceField, ModelChoiceIterator
from .models import City

class CityChoiceIterator(ModelChoiceIterator):
    def __iter__(self):
        queryset = self.queryset.select_related('country').order_by('country__name', 'name')
        groups = groupby(queryset, key=lambda x: x.country)
        for country, cities in groups:
            yield [
                country.name,
                [
                    (city.id, city.name)
                    for city in cities
                ]
            ]

class CityChoiceField(ModelChoiceField):
    iterator = CityChoiceIterator

    def __init__(self, *args, **kwargs):
        super().__init__(City.objects.all(), *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

注意:我没有时间检查该技术是否与ModelChoiceIteratorValueDjango 3.1 中引入的新技术兼容。