Need help in Python Django for fetch the records from DB which will expire in 30 days


I wrote an application where there is a requirment to display only those records which will expire is next 30 day.

class SarnarLogs(models.Model):
request_type_choice = (
    ('NAR', 'NAR'),
source = models.CharField(max_length=100)
expiry_date = models.DateField()

def __str__(self):
    return self.source

def expiry_requests(request):
Thirty_day = end_date = + timedelta(days=30)
posts = SarnarLogs.expiry_date.filter(expiry_date = Thirty_day)

context = {"console_data": posts, "user":request.user.full_name}
return render(request, 'expiry_requests.html', context=context)

>Solution :

You can use range:

posts = SarnarLogs.expiry_date.filter(expiry_date__range = (, Thirty_day))

Leave a ReplyCancel reply