Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User

后端 未结 4 1139
情书的邮戳
情书的邮戳 2020-11-30 04:42

I have an application that makes use of Django\'s UserProfile to extend the built-in Django User model. Looks a bit like:

class Us         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 05:29

    One way would be to define a custom form to use for your Team model in the admin, and override the manager field to use a queryset with the correct ordering:

    from django import forms
    
    class TeamForm(forms.ModelForm):
        manager = forms.ModelChoiceField(queryset=User.objects.order_by('username'))
    
        class Meta:
            model = Team
    
    class TeamAdmin(admin.ModelAdmin):
        list_display = ('name', 'manager')
        form = TeamForm
    

提交回复
热议问题