I
I
IvanOne2015-12-08 16:58:47
Django
IvanOne, 2015-12-08 16:58:47

Sorting queryset?

There is a ModelForm with a ModelChoiceField(queryset=Country.objects.all().order_by("name")) The countries are selected in the select, you need to move the Russia element to the very beginning of the option list, but at the same time keep the sorting, is it possible do in the form? or better through js?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2015-12-08
@IvanOne

stackoverflow.com/questions/1336900/django-modelch...

class YourForm(forms.Form):
    field1 = forms.ModelChoiceField(queryset = MyModel.objects.all() )

form = YourForm(initial = {'field1': instance_of_mymodel.pk })

or something like
queryset=[
    Country.objects.filter(name="Russia"), 
    Country.objects.exclude(name="Russia").order_by("name")]

code not tested

G
Gasoid, 2015-12-08
@Gasoid

you also need to add a field to the country model, for example
class Country(models.Model):
...
sort = models.IntegerField(default=4000)
well, assign 1 to Russia, and sort by two fields sort, name
can also be done through js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question