[Answer]-How can I hide a database entry after a set expiration date?

1đź‘Ť

Assuming you are using a DateTimeField for Post’s expiration date, you could do
this in your view:

from datetime import datetime

def upcoming(request):
    now = datetime.now()
    queryset = YourModel.objects.filter(expiration_date__gt=now)
    # your own logic here

These lines will return you all entries with an expiration_date in the future.

Expiration_date__gt stands for “expiration date greater than”. Django
automatically add these function, based on your model fields.
You also have access to __gte(greater than), __lt (lesser than), lte (lesser
than or equal). Note that you could also use the last ones to display only
expired entries with:

queryset = YourModel.objects.filter(expiration_date__lt=now)

You can find more informations in Django
docs about
field lookups
.

(Please, provide some informations on your code if you want more a specific
example)

👤Agate

Leave a comment