Answer the question
In order to leave comments, you need to log in
How to override the save method in a model with a ManyToMany field?
There is a model
class Payment(models.Model):
order = models.ManyToManyField(TicketPay, verbose_name="Заказ", blank=True, default=None)
ticket = models.ForeignKey(Ticket, verbose_name="Билет", blank=True, default=1, null=True,
on_delete=models.SET_NULL)
nmb = models.IntegerField("Количество", default=1)
price_per_item = models.DecimalField("Стоимость", default=0, max_digits=10, decimal_places=2)
total_price = models.DecimalField("Сумма", default=0, max_digits=10, decimal_places=2) # price*nmb
created = models.DateTimeField("Создано", auto_now_add=True, auto_now=False)
updated = models.DateTimeField("Обновлено", auto_now_add=False, auto_now=True)
def __str__(self):
return "%s Билета(ов). В сумме %s RUB" % (self.nmb, int(self.total_price))
class Meta:
verbose_name = "Билет в заказе"
verbose_name_plural = "Билеты в заказе"
def save(self, *args, **kwargs):
price_per_item = self.ticket.price
self.price_per_item = price_per_item
self.nmb = self.order.count()
self.total_price = int(self.nmb) * price_per_item
super(Payment, self).save(*args, **kwargs)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question