简体   繁体   中英

Sort by field in ForeignKey model

I have created a Django model called Person, which has got a 'user' ForeignKey to django.contrib.auth.models.User

How can I set the ordering on class Person to self.user.first_name, self.user.last_name?

class Meta:
    ordering = ['user__first_name', 'user__last_name']

Should do the trick iirc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM