Python 如何在Django的模型中计算产品的总价?
我想计算产品的折扣。我正在使用@property计算models.py中的价格。但我面临的问题是,product表位于product app(models.py)中,其中包含价格,而数量位于Cart app(models.py)中。对于总数,我想乘以价格*数量。我面临的错误是get_total_price没有计算金额。 view.pyPython 如何在Django的模型中计算产品的总价?,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,我想计算产品的折扣。我正在使用@property计算models.py中的价格。但我面临的问题是,product表位于product app(models.py)中,其中包含价格,而数量位于Cart app(models.py)中。对于总数,我想乘以价格*数量。我面临的错误是get_total_price没有计算金额。 view.py def cart_detail(request): cart = Carts(request) coupon_apply_form = Coupo
def cart_detail(request):
cart = Carts(request)
coupon_apply_form = CouponApplyForm()
context = {
'cart':cart,
'coupon_apply_form':coupon_apply_form
}
return render(request,'carts/coupon.html',context)
class CartItem(models.Model):
cart=models.ForeignKey('Cart',on_delete=models.SET_NULL,null=True,blank=True)
product=models.ForeignKey(Product,on_delete=models.SET_NULL,null=True,blank=True)
quantity=models.IntegerField(default=1)
class Cart(models.Model):
product = models.ManyToManyField(Product, blank=True)
total= models.DecimalField( default=0.00, max_digits=100, decimal_places=2)
class Product(models.Model):
price = models.DecimalField(decimal_places=2, max_digits=20, default=0.00)
Cart/models.py
def cart_detail(request):
cart = Carts(request)
coupon_apply_form = CouponApplyForm()
context = {
'cart':cart,
'coupon_apply_form':coupon_apply_form
}
return render(request,'carts/coupon.html',context)
class CartItem(models.Model):
cart=models.ForeignKey('Cart',on_delete=models.SET_NULL,null=True,blank=True)
product=models.ForeignKey(Product,on_delete=models.SET_NULL,null=True,blank=True)
quantity=models.IntegerField(default=1)
class Cart(models.Model):
product = models.ManyToManyField(Product, blank=True)
total= models.DecimalField( default=0.00, max_digits=100, decimal_places=2)
class Product(models.Model):
price = models.DecimalField(decimal_places=2, max_digits=20, default=0.00)
产品/型号.py
def cart_detail(request):
cart = Carts(request)
coupon_apply_form = CouponApplyForm()
context = {
'cart':cart,
'coupon_apply_form':coupon_apply_form
}
return render(request,'carts/coupon.html',context)
class CartItem(models.Model):
cart=models.ForeignKey('Cart',on_delete=models.SET_NULL,null=True,blank=True)
product=models.ForeignKey(Product,on_delete=models.SET_NULL,null=True,blank=True)
quantity=models.IntegerField(default=1)
class Cart(models.Model):
product = models.ManyToManyField(Product, blank=True)
total= models.DecimalField( default=0.00, max_digits=100, decimal_places=2)
class Product(models.Model):
price = models.DecimalField(decimal_places=2, max_digits=20, default=0.00)
在my购物车/models.py中
class Carts(object):
"""docstring for Cart"""
def __init__(self, request):
"""initalize the cart"""
self.session = request.session
cart = self.session.get(settings.CART_SESSION_ID)
if not cart:
cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart
self.coupon_id = self.session.get('coupon_id')
def __len__(self):
return sum(item['quantity'] for item in self.cart.values())
def get_total_price(self):
return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())
def clear(self):
del self.session[settings.CART_SESSION_ID]
self.session.modified = True
@property
def coupon(self):
if self.coupon_id:
return Coupons.objects.get(id=self.coupon_id)
return None
def get_discount(self):
if self.coupon:
return (self.coupon.discount / Decimal('100')) * self.get_total_price()
return Decimal('0')
def get_total_price_after_discount(self):
return self.get_total_price() - self.get_discount()
在上面的代码中,当我在折扣后从get_total_price中删除self.get_total_price时,折扣价格显示为0.00,否则显示为0.00
Cart/template.html
<table>
{% if cart.coupon %}
<tr class="gray2">
{% block trans %}
{% with code=cart.coupon.code discount=cart.coupon.discount %}
<td colspan="2">"{{code}}" coupon ({{discount}})% off</td>
{% endwith %}
{% endblock trans %}
<td colspan="4"></td>
<td class="num neg"> {{cart.get_discount|floatformat:"2"}}</td>
</tr>
{% endif %}
<tr class="total">
<td>Total</td>
<td colspan="4"></td>
<td class="num">{{cart.get_total_price_after_discount|floatformat:"2"}}</td>
</tr>
</table>
但这一切都是徒劳的,没有任何效果。请在这方面帮助我好吗
注意:
我在cart/views.py中创建了一个函数来计算总数。我可以在cart/models.py中调用使用该计算吗
提前感谢我们可以在
购物车
模型中计算聚合,如:
from decimal import Decimal
from django.db.models import F, Sum
class Cart(models.Model):
# ...
@property
def total_price(self):
return self.cartitem_set.aggregate(
total_price=Sum(F('quantity') * F('product__price'))
)['total_price'] or Decimal('0')
非常感谢您的回复。但它没有印任何东西@Willem先生甚至没有0@Samavi:模板中的
购物车
是否为购物车
对象?@Samavi:您能分享您正在使用的视图吗?您确实将模板更改为{{cart.total|u price |…}
对吗?是的,我也共享了相同的模板@Willem我编辑了我的问题购物车/models.py请查看更多信息understanding@Samavi:但问题不包含此处触发的查看功能。