Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get database table data older then 10 days in django

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

data creation dates

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.

like image 781
gamer Avatar asked Oct 18 '25 07:10

gamer


1 Answers

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.

like image 80
Ralf Avatar answered Oct 19 '25 21:10

Ralf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!