https://docs.djangoproject.com/en/1.11/ref/models/database-functions/#extract

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

From: [email protected] [mailto:[email protected]] On 
Behalf Of Dan Tagg
Sent: Tuesday, November 21, 2017 12:58 PM
To: [email protected]
Subject: Re: How to get as result a list of all the users ordered by birthday 
(month day), ignoring year

Hi Roberta,

I haven't tested it but assuming you want the ordering to happen within the 
database you may be able to do it with a combination of 
TIMESTAMPDIFF<https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff>
 and 
MOD<https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_mod>.

Something like MOD( TIMESTAMPDIFF( 'SECOND', birthday, '1900-01-01'), 31556951) 
assuming you're using MySQL.

On average there are 365.2425 days per year, which equates to 31,556,951 
seconds.

Dan

On 21 November 2017 at 15:52, Roberta Takenaka Granero 
<[email protected]<mailto:[email protected]>> wrote:
Dear Roy
I had already seen that link. But I didn't like the solution. Anyway it works, 
so I am using it for while.

Thank you

2017-11-17 11:13 GMT-02:00 Roy Shillingburg 
<[email protected]<mailto:[email protected]>>:
https://stackoverflow.com/questions/4236226/ordering-a-django-queryset-by-a-datetimes-month-day



On Friday, November 17, 2017 at 7:19:43 AM UTC-5, Roberta Takenaka Granero 
wrote:
In models:

birthday = models.DateField()

def birthday_month_day(self):
      # returns month and day
      return '12-31'


In views, I expect to see something pythonic like this:

User.objects.all().order_by('birthday_month_day')

I know it does not work because birthday_month_day is not a column in database.

The question is how to get as result a list of all the users ordered by 
birthday (month + day), ignoring year. (queryset order_by DateField month day)

Can you help me? Anyone has faced this question before?

Thanks



--
Roberta Takenaka
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/97d66095-d7a5-42c9-ad99-673fac39dac5%40googlegroups.com<https://groups.google.com/d/msgid/django-users/97d66095-d7a5-42c9-ad99-673fac39dac5%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.



--
Roberta Takenaka
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAJ9dYMGUbq-QF%3Devx7u%3DtLPatWYUKCQQ0Qd-ks9Sjs3f_kP8SA%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAJ9dYMGUbq-QF%3Devx7u%3DtLPatWYUKCQQ0Qd-ks9Sjs3f_kP8SA%40mail.gmail.com?utm_medium=email&utm_source=footer>.

For more options, visit https://groups.google.com/d/optout.



--
Wildman and Herring Limited, Registered Office: 28 Brock Street, Bath, United 
Kingdom, BA1 2LN, Company no: 05766374
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPZHCY4OyC9XnEMjSfbT6G9Zd6K1-TOa6ZPukw%3DowcLG3xUW7w%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAPZHCY4OyC9XnEMjSfbT6G9Zd6K1-TOa6ZPukw%3DowcLG3xUW7w%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ee4ced03d84e41f59f131125771cea8b%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to