I am trying to retrieve data older then 10 days to update that field data. Currently my model is like
class Restaurant(models.Model):
is_approved = models.BooleanField(null=False, default=False)
timestamp = models.DateTimeField(auto_now_add=True)
My database table is
Now when I query the database:
dish = Restaurant.objects.filter(timestamp__gt=datetime.now() - timedelta(days=10))
I get the whole table's data. I even tried to change from a day to 1 day. It still is a full database result.
If you want data that is older then you probably need to use __lt
instead of __gt
.
import datetime
from django.utils import timezone as tz
d = tz.now() - datetime.timedelta(days=10)
dish = Restaurant.objects.filter(timestamp__lt=d)
Django recomends using timezone.now()
instead of datetime.now()
to make sure that the timezone info is correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With