簡體   English   中英

如何在django的視圖文件中調用模型的函數

[英]How to call function of model in view file in django

我在 model.py 文件中創建了 def 函數,現在我想在 view.py 文件中獲取這個函數。 我知道如何在模板文件中獲取此函數,但我不知道如何在視圖文件中獲取此函數,例如:在 model.py 文件中:

 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

在view.py文件中

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()

但我收到錯誤“decimal.Decimal”對象不可調用

item_week_real_total 

應該像屬性/屬性一樣調用,而不是方法。 你用@property裝飾它,所以它應該被稱為:

cart_item_data.item_week_real_total

沒有()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM