简体   繁体   中英

How to call function of model in view file in django

I was created def function in model.py file and now i want to get this function in view.py file. I know how to get this function in template file but i dont know how to get this function in view file for example: In model.py file:

 class CartItem(models.Model):
        # cart = models.ForeignKey("Cart")
        cart = models.ForeignKey(
            'Cart',
            on_delete=models.CASCADE,
        )
        product = models.ForeignKey(product_models.Product,on_delete=models.CASCADE)
        item = models.ForeignKey(product_models.ProductDetails,on_delete=models.CASCADE)
        quantity = models.PositiveIntegerField(default=1)

        def __unicode__(self):
            return self.item.title

        @property
        def item_week_real_total(self):
            return self.item.get_week_price() * self.quantity

In view.py file

cart_item = CartItem.objects.filter(cart_id=request.session['cart_id'])
        for cart_item_data in cart_item:
            week_total = cart_item_data.item_week_real_total()

But i got error 'decimal.Decimal' object is not callable

item_week_real_total 

should be called like a property/attribute, not a method. You decorated it with @property so it should be called like:

cart_item_data.item_week_real_total

without the ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM