Django Filter Queries in List by the last x Days with Timedelta shows only Queries of substracted Day, not full Range

Viewed 62

i am trying to make a list to show the queries of the last 100 Days. If iam using Timedelta in the filter i can list the queries of the substracted Day. Only the exact Day. But i want to list all Queries between the Date in the Datefield and the Day in Timedelta Filter. What iam doing wrong?

class Appointment(models.Model):

    Appointment_Company     = models.ForeignKey(Company,   on_delete=models.CASCADE)
    Appointment_Theme       = models.CharField(max_length=200)
    Appointment_Info        = models.TextField()
    Appointment_Date        = models.DateField(default=datetime.today)


    def __str__(self):
        return self.Appointment_Theme
class AppointmentView(DetailView):


    template_name = 'timecheck/Appointment_100.html'
    model = Company

    def get_context_data(self, *args, **kwargs):

        context = super().get_context_data(**kwargs)
        context['Company_list'] = Company.objects.all()
        context['Appointment_list'] = Appointment.objects.filter(Appointment_Date=datetime.today() - timedelta ( days =  100 ))

        print (context)
        return context
1 Answers

You need to filter with a __gte lookup [Django-doc], such that Appointment_Date is greater than or equal to the given value, like:

context['Appointment_list'] = Appointment.objects.filter(
    Appointment_Date__gte=datetime.today()-timedelta(days=100)
)
Related