I have Gallery model:
class Gallery(models.Model):
TERMS_CHOICES = [
(1, '1 month'),
(3, '3 months'),
(6, '6 months'),
(12, '12 months'),
]
date = models.DateField(default=datetime.now().strftime('%Y-%m-%d'),
editable=False)
term = models.IntegerField(choices=TERMS_CHOICES, default=1)
I'm trying to create a scheduler function: it have to check expired terms, and delete the gallery. If term is null, then the term is unlimited.
How can I do it better, using django orm? Is the best way to create the list like this, or I can do it in filter? I want to use some generator comprehension like (*data, sep=' | '), but don't now how to do it correctly here:
data = [
{
'date': '2023-02-11',
'term': 1
},
{
'date': '2022-12-11',
'term': 3
},
{
'date': '2022-09-11',
'term': 6
},
{
'date': '2022-03-11',
'term': 12
},
]
result = Gallery.objects.filter(Q(**data[0]) | Q(**data[1]) | Q(**data[2]) | Q(**data[3]))
You can do something like that, more simple
from datetime import date
from dateutil.relativedelta import relativedelta
today = date.today()
conditions = Q()
for term in TERMS_CHOICES.values:
term_date = now - relativedelta(months=term)
conditions |= Q(date=term_date, term=term)
result = Gallery.objects.filter(conditions)
But i am not sure, shouldn't we rather fit the date greater than or equal to the date calculated with the term?