1

This is my schedule object:

class Schedule(Base):
    tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE, null=True)
    first_team = models.ForeignKey(Team, related_name="first_team", on_delete=models.CASCADE, null=True)
    second_team = models.ForeignKey(Team, related_name="second_team", on_delete=models.CASCADE, null=True)
    first_score = models.IntegerField(default=0, null=True)
    second_score = models.IntegerField(default=0, null=True)
    sport = models.ForeignKey(Sport, on_delete=models.CASCADE, null=True)
    date = models.DateTimeField()

I want to fetch the schedules for a specific sport, for the past 30 days for tournaments which have anything other than a 0.0 bias.

This is my query:

schedules = Schedule.objects.filter(sport=sport).filter(date__gte=date.today()).filter(
                        date__lte=(date.today() + timedelta(days=30))).order_by("date").exclude(tournament__bias=0.0)

This doesn't work. What can I try next?

4
  • "It doesn't work" isn't telling us what the problem is. What do you get? An error? Commented Feb 13, 2019 at 17:53
  • @dirkgroten it fetched an empty set Commented Feb 13, 2019 at 17:54
  • 1
    You're now fetching dates between today and today + 30 (15 March), so dates in the future. Is that what you want? You said you wanted dates in the past. Commented Feb 13, 2019 at 17:54
  • 1
    To debug this query, remove one of your criteria at a time and then check your queryset. That'll help you figure out which of the criteria is filtering out everything and leaving you with your empty set. Agree with @dirkgroten that your problem is likely adding dates instead of subtracting. Commented Feb 13, 2019 at 17:59

3 Answers 3

3

Your code is correct way of filtering (although you could merge the two filter() methods by comma-separating the lookups).

The problem might be that you're now filtering for:

today() <= date <= today() + 30 days 

So if you don't have any instances with a date in the future 30 days (or today), you'll get an empty set.

Sign up to request clarification or add additional context in comments.

Comments

1

Your chaining filter looks like almost correct but i guess you messing with past 30 days section filtering.

schedules = Schedule.objects.filter(sport=sport).filter(date__lt=date.today()).filter(
                        date__gte=(date.today() - timedelta(days=30))).order_by("date").exclude(tournament__bias=0.0)

Comments

1

Just to be sure, you should be using tz.now() instead of date.today(), because you probably have USE_TZ = True (the default value). See the django docs.

Also, you have to invert your date filters, because you are searching for 30 days into the future, not the past.

import datetime
from django.utils import timezone as tz

today = tz.localtime(tz.now()).date()
start_date = today - datetime.timedelta(days=30)
end_date = today

schedules = Schedule.objects.exclude(tournament__bias=0.0) \
    .filter(sport=sport, date__gte=start_date, date__lte=end_date) \
    .order_by("date")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.