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

Question:

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

models.py

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

Views.py

@login_required(login_url=('/login'))
def expiry_requests(request):
Thirty_day = end_date = datetime.now().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)
Asked By: Rizwan

||

Answers:

You can use range:

posts = SarnarLogs.expiry_date.filter(expiry_date__range = (datetime.now().date(), Thirty_day))
Answered By: ruddra
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.