Home > database >  Django: unsupported operand type(s) for : 'int' and 'method' in django
Django: unsupported operand type(s) for : 'int' and 'method' in django

Time:04-13

i am trying to write a function that would return cart total but it keep showing this error unsupported operand type(s) for : 'int' and 'method'when i comment this code out total = sum([item.get_total for item in orderitems]) the errors goes away but i dont get my cart total, which means the code is needed. i found out that it might be because i am trying to use a Concatenation " " with String and different types is not allowed in Pyhton. but i can't pin point where the error is coming from!

models.py



class Order(models.Model):
    student = models.ForeignKey(Student, on_delete=models.SET_NULL, null=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    completed = models.BooleanField(default=False, null=True, blank=True)
    transaction_id = models.CharField(max_length=200, null=True, blank=True)

    def __str__(self):
        return self.student.user.username
        # return str(self.id)
    
    @property
    def get_cart_total(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.get_total for item in orderitems])
        return total

    @property
    def get_cart_items(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.quantity for item in orderitems])
        return total

CodePudding user response:

You forgot to call method item.get_total()

sum([item.get_total() for item in orderitems])
  • Related