In DJANGO, how do I query for records that do not exist after a certain date?
Using .exclude(…)
[Django-doc] on the Invoice
model will not work: it will not include any Customer
s that have never bought anything. But these will still include people that bought something before: indeed, if a Customer
bought 100 days ago something, and then 20 days ago, that Customer
will still be included, since the Invoice
for the second transaction is included, but the one 100 days ago will still be retained.
You can work on the Customer
model with .exclude(…)
with:
from datetime import timedelta
from django.utils.timezone import now
Customer.objects.exclude(
invoices__date__gte=now()-timedelta(days=30)
)
This will return a QuerySet
of Customer
s that are not related to an Invoices
record in the last 30 days.