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

Time:12-25

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)

CodePudding user response:

You can use range:

posts = SarnarLogs.expiry_date.filter(expiry_date__range = (datetime.now().date(), Thirty_day))
  • Related