Ordering a Django QuerySet by a datetime's month/day?

前端 未结 5 631
太阳男子
太阳男子 2021-01-02 16:00

I have a list of people, each person having a birthdate, which is predictably stored in a DateField. I\'m trying to create a list of those people—sorted by the

5条回答
  •  日久生厌
    2021-01-02 16:42

    Newer versions of django have the lookup on DateFields and DateTimeFields. https://docs.djangoproject.com/en/1.11/ref/models/database-functions/#extract

    MyModel.objects.order_by('birthday__month', 'birthday__day')

提交回复
热议问题