Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change sort order of modelchoicefield django

Tags:

django-forms

how can I change the order of the values in the ModelChoiceField billing_company?

models.py

class Company(models.Model):
    name = models.CharField(max_length=30, unique=True)

forms.py

billing_company = forms.ModelChoiceField(Company.objects, required=True)

Thanks for your help. Tom

like image 778
Thomas Kremmel Avatar asked Jul 11 '11 20:07

Thomas Kremmel


1 Answers

ModelChoiceField takes a QuerySet as its first parameter, so you should be able to pass an ordered set:

forms.ModelChoiceField(Company.objects.order_by('-pk'), required=True)
like image 68
Jack M. Avatar answered Oct 22 '22 04:10

Jack M.