I have the following models:
class RegionCategory(models.Model):
"""Categories of regions"""
symbol = models.CharField(max_length=15)
description = models.CharField(max_length=50)
class Region(models.Model):
"""Region model"""
region = models.CharField(max_length=30)
category = models.ForeignKey(RegionCategory)
class md_orderlog(models.Model):
"""Order log model"""
order_region = models.ForeignKey(Region)
Nothing uncommon, just a model with a foreign key to another model.
I have a ModelForm also:
class md_orderlogForm(forms.ModelForm):
"""Model form of md_orderlog model"""
def __init__(self, *args, **kwargs):
super(md_orderlogForm, self).__init__(*args, **kwargs)
self.fields['order_region'] = forms.ModelChoiceField(
label=("Project Region"), widget=forms.Select,
queryset=Region.objects.all())
What I want and I don't know how to do is to customize the order_region field in the form so the select show the Region items divided by the category, something like this:
Category1
Region1
Region2
Region3
Category2
Region4
Region5
Region6
Any ideas?, thanks!