How do I specify an order of values in drop-down list in a Django ModelForm?

前端 未结 1 1834
我寻月下人不归
我寻月下人不归 2021-01-18 09:21

OK, here is the question.

class UserForm(forms.ModelForm):   

    class Meta:
        model = User
        fields = (\'team\',  \'related_projects\') 
         


        
1条回答
  •  迷失自我
    2021-01-18 10:15

    You'd need to specify an override for the team field and then you should be able to override the order with its queryset argument. Here I'm assuming the property in Team you want to sort on is name.

    class UserForm(forms.ModelForm):
        team = forms.ModelChoiceField(queryset=Team.objects.order_by('name'))
    
        class Meta:
            model = User
            fields = ('team',  'related_projects') 
    

    You can read more about ModelChoiceField here: http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield

    0 讨论(0)
提交回复
热议问题