简体   繁体   中英

How do I do this “order by” in Django, if I have foreign keys?

Suppose my model is this:

class Ego(models.Model):
    event = models.ForeignKey(Event)
    user = models.ForeignKey(User)

As you can see, this table has 2 columns, and they're both foreign keys. How do I "order by" User.first_name?

Is this it? But it doesn't look like it.

Ego.objects.all().order_by("User.first_name")

Solved.

I did this:

Ego.objects.all().select_related.order_by("auth_user.first_name")

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