Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User python python

Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User


This

class Meta:    ordering = ['username']

should be

    ordering = ['user__username']

if it's in your UserProfile admin class. That'll stop the exception, but I don't think it helps you.

Ordering the User model as you describe is quite tricky, but see http://code.djangoproject.com/ticket/6089#comment:8 for a solution.


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 formsclass TeamForm(forms.ModelForm):    manager = forms.ModelChoiceField(queryset=User.objects.order_by('username'))    class Meta:        model = Teamclass TeamAdmin(admin.ModelAdmin):    list_display = ('name', 'manager')    form = TeamForm


This might be dangerous for some reason, but this can be done in one line in your project's models.py file:

User._meta.ordering=["username"]