[Answered ]-Django form QuerySet for ModelChoiceField

2👍

Does contact_country = forms.ModelChoiceField(queryset=Country.objects.order_by('special')) work?

0👍

This is untested, so you may give it a try, but it may not give it what you want…

in your view do this:

specials = Country.objects.filter(special=True)
all_of = Country.objects.all()

# worst thing is, this is a list, not a queryset...
new_list = list(specials)+list(all_of)

# add new object to you form...
YourForm.base_fields['contact_country'] = forms.ChoiceField(choices=[(x.id,x) for x in new_list])

Your handicap is, yo create the list using a list, not directly from queryset

This will solve your pronblem, with a dirty looking way, but it is a solution i used when modelform can not help you…

👤Mp0int

0👍

You can override the default ordering:

class Meta:
    ordering = [ '-special', 'printable_name' ]

You can also write a custom manager but it doesn’t worth…

👤Don

Leave a comment