python - Can i do an order_by on day in month? -
i trying create list of birth days, ordered dates, query set is:
birth_days = get_list_or_404(userprofile.objects.order_by('-birthdate'), birthdate__month=datetime.datetime.now().month)
how can list ordered day in month birth days list ordered, right ordered date, if 2 people has birth day month , person 1 date lets 20/7/1980 , person 2 date 17/7/1970, person 2 still after person 1 in list. don't care year of birth, month , day in month....
can in order_by this:
order_by('-birthdate__day')
i know can't because isn't working, asking if there way (in view, not in model in case...not think matters on here, saying)?
thank you, erez
you can extract day "extra" field , sort on that, example:
userprofile.objects.select(extra={'birth_day': 'extract(day birthdate)'}).order_by('-birth_day')
this should work postgresql @ least.
Comments
Post a Comment