我正在制作比萨饼应用程序,任务是对不同大小的比萨饼有不同的价格。而且,如果我在披萨上添加浇头,价格会更高。
我有一个set_price函数,用于设置披萨的价格。我需要做的是每次添加新的浇头和每次创建新的Pizza对象时都调用此set_price函数。
class Topping(models.Model):
name = models.CharField(max_length=64)
def __str__(self):
return f"{self.name}"
class RegPizza(models.Model):
size = models.CharField(max_length=5)
price = 0
toppings = models.ManyToManyField(Topping, blank=True, related_name="inreg")
def __str__(self):
return f"Regular Pizza ({self.price}$)"
def set_price(self):
if self.size == 'Small':
self.price = 12.20
if self.toppings.count() == 1:
self.price = 13.20
if self.toppings.count() == 2:
self.price = 14.70
if self.toppings.count() == 3:
self.price = 15.70
if self.size == 'Large':
self.price = 17.45
if self.toppings.count() == 1:
self.price = 19.45
if self.toppings.count() == 2:
self.price = 21.45
if self.toppings.count() == 3:
self.price = 23.45