【问题标题】:IntegrityError at order/order/add/ null value in column "total_incl_tax" violates not-null constraint DETAIL: Failing row contains“total_incl_tax”列中订单/订单/添加/空值的 IntegrityError 违反非空约束细节:失败行包含
【发布时间】:2022-01-23 12:51:07
【问题描述】:

当我尝试将价格添加到 django oscar 时出现此错误,并且此字段在模型中不可编辑或不可写入

我需要帮助才能完成这项工作 IntegrityError at order/order/add/null value in column "total_incl_tax" 违反非空约束细节:失败行包含

class AbstractOrder(models.Model):
    """
    The main order model
    """
    number = models.CharField(
        _("Order number"), max_length=128, db_index=True, unique=True)

    # We track the site that each order is placed within
    site = models.ForeignKey(
        'sites.Site', verbose_name=_("Site"), null=True,
        on_delete=models.SET_NULL)

    basket = models.ForeignKey(
        'basket.Basket', verbose_name=_("Basket"),
        null=True, blank=True, on_delete=models.SET_NULL)

    # Orders can be placed without the user authenticating so we don't always
    # have a customer ID.
    user = models.ForeignKey(
        AUTH_USER_MODEL, related_name='orders', null=True, blank=True,
        verbose_name=_("User"), on_delete=models.SET_NULL)

    # Billing address is not always required (eg paying by gift card)
    billing_address = models.ForeignKey(
        'order.BillingAddress', null=True, blank=True,
        verbose_name=_("Billing Address"),
        on_delete=models.SET_NULL)

    # Total price looks like it could be calculated by adding up the
    # prices of the associated lines, but in some circumstances extra
    # order-level charges are added and so we need to store it separately
    currency = models.CharField(
        _("Currency"), max_length=12, default=get_default_currency)
    total_incl_tax = models.DecimalField(
        _("Order total (inc. tax)"), decimal_places=2, max_digits=12)
    total_excl_tax = models.DecimalField(
        _("Order total (excl. tax)"), decimal_places=2, max_digits=12)

    # Shipping charges
    shipping_incl_tax = models.DecimalField(
        _("Shipping charge (inc. tax)"), decimal_places=2, max_digits=12,
        default=0)
    shipping_excl_tax = models.DecimalField(
        _("Shipping charge (excl. tax)"), decimal_places=2, max_digits=12,
        default=0)

    # Not all lines are actually shipped (such as downloads), hence shipping
    # address is not mandatory.
    shipping_address = models.ForeignKey(
        'order.ShippingAddress', null=True, blank=True,
        verbose_name=_("Shipping Address"),
        on_delete=models.SET_NULL)
    shipping_method = models.CharField(
        _("Shipping method"), max_length=128, blank=True)

    # Identifies shipping code
    shipping_code = models.CharField(blank=True, max_length=128, default="")

    # Use this field to indicate that an order is on hold / awaiting payment
    status = models.CharField(_("Status"), max_length=100, blank=True)
    guest_email = models.EmailField(_("Guest email address"), blank=True)

【问题讨论】:

    标签: django django-oscar


    【解决方案1】:

    你是如何添加它的?模型字段是这个:

    total_incl_tax = models.DecimalField(
        _("Order total (inc. tax)"), decimal_places=2, max_digits=12)
    

    这不能为空,如果我查看您的错误,您正在尝试创建一个在 total_incl_tax 字段中设置为空的订单。

    此外,要创建订单,oscar 中有一个 OrderCreator 类: https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/order/utils.py#L37

    其中有一个 place_order 方法,您应该使用它来创建订单。

    不建议手动创建 Order 对象,因为订单有行、折扣等。

    创建 OrderCreator 类的目的是,您猜对了:创建订单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2016-01-18
      • 2020-08-20
      • 2019-04-08
      相关资源
      最近更新 更多