Django - sort by userprofile field in admin

I figured out how to get information about UserProfile to appear in my admin field thanks to some help from you guys. Now I want to come up with a way to correctly sort these fields. I'm near.

class EmployerProfileAdmin(UserAdmin):
    inlines = [EmployerProfileInline, ]
    def company(self):
        return self.get_profile().company
    company.admin_order_field = 'employerprofile'
    list_display = ('username','first_name','last_name',company,'email','password',)

Let me sort the company field from my UserProfile (EmployerProfile), but I need to sort it according to the field in User. The user has a "joberprofile", but the entire object. How django can sort by this field? Can I overwrite it in the EmployerProfile class?

Thank!

+3
source share
2 answers

As Honza said, what are you trying to order?

admin ModelAdmin.ordering .

+1

, model__field? , ,

class Meta:
    ordering = [ "user__fieldname" ]

( ). , ForeignKey, .

- :

class User( object ):
    fieldname = models.CharField( max_length = 255 )

class Profile( object )
     class Meta:
         ordering = [ "user__fieldname" ]
     user = models.ForeignKey( User )
+1

Source: https://habr.com/ru/post/1782945/


All Articles