Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User

后端 未结 4 1130
情书的邮戳
情书的邮戳 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:42

    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)
    

提交回复
热议问题