Answer the question
In order to leave comments, you need to log in
How to refer to a function in a model?
Hello.
Question for the experts. I want to remove a condition from the template into the model.
models.py
master_promo_count = models.SmallIntegerField(default=0, verbose_name='Количество дней акции',)
# СЧЕТЧИК АКЦИИ
def promo(self, *args, **kwargs):
self.master_promo_count = (self.master_date_end - datetime.today().date()).days
return self.master_promo_count
# Замена словаря в шаблоне
def master_promo_count_day(self, *args, **kwargs):
if self.promo(self, *args, **kwargs) == 1:
return f'день'
elif self.promo(self, *args, **kwargs) == 2:
return f'дня'
else:
return f'дней'
<span class="red-text bb f18">{{ obj.promo }} {{ obj.master_promo_count_day }}</span></div>
# Замена словаря в шаблоне
def master_promo_count_day(self):
if self.master_promo_count == 1:
return f'день'
elif self.master_promo_count == 2:
return f'дня'
else:
return f'дней'
Answer the question
In order to leave comments, you need to log in
You generally have a game with the code ...
This is:
master_promo_count = models.SmallIntegerField(default=0, verbose_name='Количество дней акции',)
# СЧЕТЧИК АКЦИИ
def promo(self, *args, **kwargs):
self.master_promo_count = (self.master_date_end - datetime.today().date()).days
return self.master_promo_count
@property
def master_promo_count(self):
return (self.master_date_end - datetime.today().date()).days
def master_promo_count_day(self):
if self.master_promo_count == 1:
return f'день'
elif self.master_promo_count == 2:
return f'дня'
else:
return f'дней'
<span class="red-text bb f18">{{ obj.promo }} {{ obj.master_promo_count_day }}</span></div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question