I have the following code in the model, while calling the get_cart_total int not callable or unsupported operand type(s) for : 'int' and 'method'appaers
I am expecting to get total from get_cart_total
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
date_ordered = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False, null=True, blank=False)
transaction_id = models.CharField(max_length=200, null=True)
def __str__(self):
return str(self.id)
@property
def get_cart_items(self):
orderitems = self.orderitem_set.all()
total = sum([item.quantity for item in orderitems])
return total
@property
def get_cart_total(self):
items = self.get_cart_items()
for item in items:
total = item.get_total()
return total
class OrderItem(models.Model):
ORDER_ITEM_TYPE = (
('type1', 'Collection1'),
('type2', 'Collection2'),
)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
collection_type = models.CharField(max_length=255, choices=ORDER_ITEM_TYPE)
collection1 = models.ForeignKey(Collection1, on_delete=models.SET_NULL, null=True, blank=True)
collection2 = models.ForeignKey(Collection2, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.IntegerField()
def get_total(self):
if self.collection_type == "type1":
return self.collection1.price * self.quantity
elif self.collection_type == "type2":
return self.collection2.price * self.quantity
CodePudding user response:
Since self.get_cart_items
is a property, you do not need to call it. You can either remove the @property
decorator, or just expect self.get_cart_items
to be an integer already, not a method.
@property
def get_cart_total(self):
- items = self.get_cart_items()
items = self.get_cart_items
for item in items:
total = item.get_total()
return total
CodePudding user response:
You have set get_cart_items
as property
which is treated as an attribute of an object, not as a method in that matter. You need to delete that decorator or ()
from your calculations.
Option 1:
@property
def get_cart_items(self):
orderitems = self.orderitem_set.all()
total = sum([item.quantity for item in orderitems])
return total
@property
def get_cart_total(self):
items = self.get_cart_items
for item in items:
total = item.get_total()
return total
Option 2:
def get_cart_items(self):
orderitems = self.orderitem_set.all()
total = sum([item.quantity for item in orderitems])
return total
@property
def get_cart_total(self):
items = self.get_cart_items()
for item in items:
total = item.get_total()
return total
CodePudding user response:
- Your
get_cart_items
is a property method, which is not callable, so remove()
when you want to use this method get_cart_items()
is returning an int value which is the sum of item quantities, so here on your codeitems = self.get_cart_items()
you can't loop through an integer value
Fix your code accordingly
@property
def get_cart_items(self):
return self.orderitem_set.all()
@property
def get_cart_total(self):
items = self.get_cart_items
for item in items:
total = item.get_total()
return total
CodePudding user response:
Couple of points:
- 'get_cart_items' is a property (@property decorator) so you should treat it as a such. Change:
items = self.get_cart_items()
toitems = self.get_cart_items
, or remove the @property decorator. - 'get_cart_items' is not returning a list of OrderItem objects, but a sum of item quantity (an integer)
- The 'total' variable is not initialized in 'get_cart_total' method.
Try something like this for 'get_cart_total' method:
def get_cart_total(self):
total = 0
for item in self.orderitem_set.all():
total = item.get_total()
return total