Reputation: 21
I want to subtract available_quantity to total_quantity . but i get this error how can i solve it
class Order(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
distribute_price = models.IntegerField()
mrp_price = models.IntegerField(null=True)
created_at = models.DateTimeField(auto_now_add=True)
user_give_quantity = models.IntegerField(null=True)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
def __str__(self):
return self.product.item_name
def aaa_qqq(self):
return self.mrp_price * self.user_give_quantity
def available_quantity(self):
return self.product.quantity - self.user_give_quantity if all([self.product,
self.product.quantity, self.user, self.user_give_quantity]) else 0
def total_quantity(self):
return self.available_quantity - self.user_give_quantity
Upvotes: 1
Views: 490
Reputation: 476534
The reason that this happens is because self.available_quantity
is a method, not a property. You thus should call the method:
def total_quantity(self):
return self.available_quantity() - self.user_give_quantity
Note that if user_give_quantity
is NULL
/None
, then this will raise an error as well. You thus should check this, for example with:
def total_quantity(self):
if self.user_give_quantity is not None:
return self.available_quantity() - self.user_give_quantity
Then total_quantity()
will return None
if the self.user_give_quantity
is None
.
You might want to clean up the available_quantity
as well:
def available_quantity(self):
if self.product and self.product.quantity and self.user and self.user_give_quantity is not None:
return self.product.quantity - self.user_give_quantity
return 0
It is furthermore not entirely clear to me how available_quantity
will work here: if there are multiple Order
s that each need some quantity, then you normally need to add up the quantities of all orders, here you only focus on the quantity of a specific order.
You can also use the @property
decorator [python-doc] to define these as properties, then you can work with self.available_quantity
:
class Order(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
distribute_price = models.IntegerField()
mrp_price = models.IntegerField(null=True)
created_at = models.DateTimeField(auto_now_add=True)
user_give_quantity = models.IntegerField(null=True)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
def __str__(self):
return self.product.item_name
@property
def aaa_qqq(self):
return self.mrp_price * self.user_give_quantity
@property
def available_quantity(self):
if self.product and self.product.quantity and self.user and self.user_give_quantity is not None:
return self.product.quantity - self.user_give_quantity
return 0
@property
def total_quantity(self):
if self.user_give_quantity is not None:
return self.available_quantity - self.user_give_quantity
Upvotes: 3