pythondjangodjango-orm

Django query chaining multiple filters


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?


Solution

  • 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)