Search code examples
pythondjangodjango-modelsdjango-viewspython-datetime

How to get the users with birth day that happen at this month?


I am trying to get all the user with birth day of this month:

Did this but getting errors, Can any one help with what am i doing wrong?

birth_days = get_list_or_404(UserProfile, birthDate.month() == datetime.date.month())

I've tried many other things, but can't make it work...

thank you, Erez


Solution

  • There are several problems

    1. datetime.date.month() does not exist, i think you mean datetime.datetime.now().month
    2. You are not using the right criteria, again birtDay.month() does not exist, i think you should use

      birth_days = get_list_or_404(UserProfile, birthDate__month=datetime.datetime.now().month)

    JFTR have a look at https://docs.djangoproject.com/en/dev/ref/models/querysets/#month