-3

How can I filter the model data according to date range of two columns - birthday and anniversary which are datefields using Django querysets? This is in a function that is called every friday. So the date range will be from tomorrow i.e., Saturday to next Friday.

def mailer_function():
    model_data = Model.objects.filter(
                  birthday__range=["tomorrow(i.e., Saturday's date)", "next friday's date"],
                  anniversary__range=["tomorrow(i.e., Saturday's date)", "next friday's date"]
                 )

Here I should get the model_data whose birthday or anniversary dates are in the next week.

user001
  • 425
  • 4
  • 21
  • Does this answer your question? [Set up a scheduled job?](https://stackoverflow.com/questions/573618/set-up-a-scheduled-job) – quqa123 Nov 30 '20 at 09:30
  • Yes but only the first part. It does not answer how to filter for the next week using django querysets. – user001 Nov 30 '20 at 09:34
  • You did 0 work researching answers to your question and want someone else to solve it for you. There dozens of duplicates of your questions already answered on stackoverflow – quqa123 Nov 30 '20 at 14:22

2 Answers2

2

You can use datetime.timedelta

import datetime

def next_week():
    today = datetime.date.today()
    return [
        today + datetime.timedelta(days=1),
        today + datetime.timedelta(days=7)
    ]

And if you want to filter birthday OR anniversary, check Q objects

from django.db.models import Q

queryset = Model.objects.filter(
    Q(birthday__range=next_week()) | Q(anniversary__range=next_week())
)

To schedule use your favorite external tool, something like cron. I suggest to create your task as a management command

Txema
  • 849
  • 5
  • 15
0

You can do this by calling the program on a scheduled basis using an external program like anacron.

I would suggest to use Celery for this as it keeps all your scheduled tasks internal to your project.

Nicolas Appriou
  • 2,208
  • 19
  • 32
  • Ok. That will solve the first part. How can I filter for the next week starting from saturday to next friday? – user001 Nov 30 '20 at 09:28