django order by related field

前端 未结 2 444
说谎
说谎 2021-01-04 01:23

I want to sort a QuerySet of contacts by a related field. But I do not know how. I tried it like this, but it does not work.

foundContacts.order_by(\"classif         


        
相关标签:
2条回答
  • 2021-01-04 01:57

    It should be:

    foundContacts.order_by("classification__kam")
    

    Here is a link for the Django docs on making queries that span relationships: http://docs.djangoproject.com/en/1.1/topics/db/queries/#lookups-that-span-relationships

    You can also see some examples in the order_by reference:
    https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.order_by

    0 讨论(0)
  • 2021-01-04 02:09

    as the documentation indicates, it should be used as in queries about related models

    https://docs.djangoproject.com/en/3.0/ref/models/querysets/#order-by

    foundContacts.order_by("classification__kam")
    
    0 讨论(0)
提交回复
热议问题