Ordering Choices in ModelForm ManytoManyField DJANGO

Viewed 12220

I have in my models.py

class Business(models.Model):
   industry = models.models.ManyToManyField(Industry)

in forms.py

class BusinessForm(forms.ModelForm):
    class Meta:
        model = Business

When I render the form, the industry names appear in a multiple select box. What do I do to make the industry names in alphabetical order?

2 Answers

I like this method:

class BusinessForm(forms.ModelForm):

    class Meta:
        model = Business

    industry = forms.ModelMultipleChoiceField(
               queryset=Industry.objects.order_by('name'))

I like it, since it does not alter the database model, and since it is declarative (less programming).

Related