DJANGO: тег optgroup ModelChoiceField
Как я могу установить в ModelChoiceFieldOPTGROUP тег?
Это пример:
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
forms.py
class LinkForm(ModelForm):
config = ModelChoiceField(queryset=Config.objects.all(), empty_label="Choose a link",widget=GroupedSelect())
class Meta:
model = Link
Я хотел бы сделать мой ChoiceField следующим образом:
example.html
Choose a link
Address: 192.168.1.202/255.255.255.0
Address: 192.168.1.240/255.255.255.0
Address: 192.168.3.1/255.255.255.0
**ОБНОВИТЬ**
Я решил свою проблему следующим образом: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('', 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'%s' % (escape(option_value), escape(option_label)))
output.append('')
elif index!=0 and index