嗨,我正在试着编辑Django-Shop的演示。
我使用Python 3.7.5安装了Django-Shop的1.2.4版和Django 3.0.14版。
在我的例子中,我也想按重量销售产品。正如documentation中所说的,我必须重写CartItem。
from django.db import models
from django.utils.translation import ugettext_lazy as _
from shop.models import cart
class CartItem(cart.BaseCartItem):
quantity = models.DecimalField(_("Cart item quantity"), decimal_places=3, max_digits=3)我在我的models.py中导入了这个CartItem。在那之后,我还必须重写OrderItem类中的数量。
class OrderItem(BaseOrderItem):
quantity = models.DecimalField(_("Ordered quantity"), decimal_places=3, max_digits=3)
# quantity = models.PositiveIntegerField(_("Ordered quantity"))
canceled = models.BooleanField(_("Item canceled "), default=False)
def populate_from_cart_item(self, cart_item, request):
super().populate_from_cart_item(cart_item, request)
# the product's unit_price must be fetched from the product's variant
try:
variant = cart_item.product.get_product_variant(
product_code=cart_item.product_code)
self._unit_price = Decimal(variant.unit_price)
except (KeyError, ObjectDoesNotExist) as e:
raise CartItem.DoesNotExist(e)现在我可以毫无问题地运行服务器了,但是我不能在购物车中添加任何东西。
我不知道该怎么办..。
发布于 2021-09-26 00:02:15
如果将models.DecimalField替换为models.FloatField,则可以按预期将产品添加到购物车中。显然,文档中明确提到了DecimalField是错误的。
https://stackoverflow.com/questions/69329553
复制相似问题