首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Django-Shop CartItems中使用小数

如何在Django-Shop CartItems中使用小数
EN

Stack Overflow用户
提问于 2021-09-25 19:55:29
回答 1查看 30关注 0票数 0

嗨,我正在试着编辑Django-Shop的演示。

我使用Python 3.7.5安装了Django-Shop的1.2.4版和Django 3.0.14版。

在我的例子中,我也想按重量销售产品。正如documentation中所说的,我必须重写CartItem

代码语言:javascript
复制
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类中的数量。

代码语言:javascript
复制
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)

现在我可以毫无问题地运行服务器了,但是我不能在购物车中添加任何东西。

我不知道该怎么办..。

EN

回答 1

Stack Overflow用户

发布于 2021-09-26 00:02:15

如果将models.DecimalField替换为models.FloatField,则可以按预期将产品添加到购物车中。显然,文档中明确提到了DecimalField是错误的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69329553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档