Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User

荒凉一梦 提交于 2019-11-26 12:44:47

问题


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

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    # Local Stuff
    image_url_s = models.CharField(max_length=128, blank=True)
    image_url_m = models.CharField(max_length=128, blank=True)

    # Admin
    class Admin: pass

I have added a new class to my model:

class Team(models.Model):
    name = models.CharField(max_length=128)
    manager = models.ForeignKey(User, related_name=\'manager\')
    members = models.ManyToManyField(User, blank=True)

And it is registered into the Admin:

class TeamAdmin(admin.ModelAdmin):
    list_display = (\'name\', \'manager\')

admin.site.register(Team, TeamAdmin)

Alas, in the admin inteface, when I go to select a manager from the drop-down box, or set team members via the multi-select field, they are ordered by the User numeric ID. For the life of me, I can not figure out how to get these sorted.

I have a similar class with:

class Meta:
    ordering = [\'name\']

That works great! But I don\'t \"own\" the User class, and when I try this trick in UserAdmin:

class Meta:
    ordering = [\'username\']

I get:

django.core.management.base.CommandError: One or more models did not validate: events.userprofile: \"ordering\" refers to \"username\", a field that doesn\'t exist.

user.username doesn\'t work either. I could specify, like image_url_s if I wanted to . . . how can I tell the admin to sort my lists of users by username? Thanks!


回答1:


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



回答2:


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.




回答3:


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"]



回答4:


For me, the only working solution was to use Proxy Model. As stated in the documentation, you can create own proxy models for even built-in models and customize anything like in regular models:

class OrderedUser(User):
    class Meta:
        proxy = True
        ordering = ["username"]
    def __str__(self):
        return '%s %s' % (self.first_name, self.last_name)

After that, in your model just change Foreign Key to:

user = models.OneToOneField(OrderedUser, unique=True)

or even more suitable

user = models.OneToOneField(OrderedUser, unique = True, parent_link = True)


来源:https://stackoverflow.com/questions/1474135/django-admin-ordering-of-foreignkey-and-manytomanyfield-relations-referencing-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!