Filter a model by date range of next week in Django
NickName:user001 Ask DateTime:2020-11-30T17:25:18

Filter a model by date range of next week in Django

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.

Copyright Notice:Content Author:「user001」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65070923/filter-a-model-by-date-range-of-next-week-in-django

Answers
Txema 2020-11-30T11:38:25

You can use datetime.timedelta\nimport datetime\n\ndef next_week():\n today = datetime.date.today()\n return [\n today + datetime.timedelta(days=1),\n today + datetime.timedelta(days=7)\n ]\n\nAnd if you want to filter birthday OR anniversary, check Q objects\nfrom django.db.models import Q\n\nqueryset = Model.objects.filter(\n Q(birthday__range=next_week()) | Q(anniversary__range=next_week())\n)\n\nTo schedule use your favorite external tool, something like cron. I suggest to create your task as a management command",


More about “Filter a model by date range of next week in Django” related questions

Filter a model by date range of next week in Django

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 ...

Show Detail

Django: Filter data based on expiration date coming within next 1 week and 2 weeks

I have a model class: class Products(models.Model): product = models.Charfield( field specs here . ) expiration_date = modelsDateTimeField ( . field specs here . ) Now I want the users...

Show Detail

Filter range date from custom property using SimpleListFilter

Until now I've been using django-daterange_filter for filtering dates on a range in Django Admin. That works really well as long as the date is a field from the model. However, now my date is a pr...

Show Detail

Filter Django query by week of month?

In a Django query, how would you filter by a timestamp's week within a month? There's a built-in week accessor, but that refers to week-of-the-year, e.g. 1-52. As far as I can tell, there's no other

Show Detail

django : Model filter on date ranges

How to get model filter on date ranges. In my project using employee doj, i need to deign a table. like employee who have joined less than three months , 3-6month, 6-12 months, 12-24 months. Depar...

Show Detail

Django - Filter a date within a range with validation

I have a DateTimeField named 'session_start' in a model. I am trying to filter the session_start field within a date range. Model.py class TimeCard(models.Model): profile = models.ForeignKey(

Show Detail

How to put placeholder in Django date range filter

I am using Django filters to search by giving date range in my application. class AdvancedSearchListFilter(django_filters.FilterSet): received = django_filters.DateFromToRangeFilter(label='Rec...

Show Detail

TSQL Date range for this week and next week

I am working on a query where I need to provide a total number of meetings that fall in the date range of the current week as well as the number that falls in the range of next week. A week for us...

Show Detail

Django: filter datetime in date on date range

I'm trying to filter the query set on between two dates. here is my views.py week = (startDate, endDate) Student.objects.filter(created__range=week) startDate and endDate is date objects created ...

Show Detail

next and previous week with Django 1.3 generic views week archive

I am new to Django and I would like to know how can I get the next and previous week links in my template using the week archive generic view. For the archive_month generic view there is a next_mon...

Show Detail