【问题标题】:Using Django F expression with Case, When expression将 Django F 表达式与 Case、When 表达式一起使用
【发布时间】:2020-11-20 07:43:03
【问题描述】:

我有一个类似的 Django 模型

# app/models.py

class tbl_invoice(models.Model):
    invoice_id                      = models.IntegerField(blank=True, null=True)
    client_id                       = models.ForeignKey(tbl_customer, on_delete=models.CASCADE)
    invoice_number                  = models.IntegerField(blank=True, null=True)
    date                            = models.DateField(blank=True, null=True)
    amount                          = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
    paid_amount                     = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
    balance                         = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
    date_of_payment                 = models.DateField(blank=True, null=True)
    status                          = models.CharField(max_length=50, default='', blank=True, null=True)

我有另一个保存付款记录的视图,我想根据用户输入的差异将status 更新为 PaidUnpaid金额和balance 是否为0

目前,这就是我所做的

#app/views.py

if form.is_valid():
    post = form.save(commit=False)
      # rest of the logic goes here
    post.save()
 # this data is saved in another model, after that I update invoice model

tbl_invoice.objects.filter(invoice_id=someIdHere).update(paid_amount=F('paid_amount')+post.amount, 
      date_of_payment=post.date, balance=F('balance')-post.amount, 
      status=Case(
      When(balance=F('balance')-post.amount==0, then=Value("Paid")), 
      When(balance=F('balance')-post.amount!=0, then=Value("Unpaid")), 
   ))

此查询将status 更新为空白。

但是,仅使用 F 表达式来更新 amountbalance 工作正常

tbl_invoice.objects.filter(invoice_id=someIdHere).update(paid_amount=F('paid_amount')+post.amount, 
      date_of_payment=post.date, balance=F('balance')-post.amount

以上语句运行良好

我肯定犯了一些我无法弄清楚的明显错误。 如何将F 表达式与Case 一起使用??

【问题讨论】:

标签: django django-models orm


【解决方案1】:

首先,请阅读与您的Django版本对应的When document。 可以检查以下代码作为解决方案:

tbl_invoice.objects.filter(invoice_id=someIdHere).update(paid_amount=F('paid_amount') + post.amount,
                                                     date_of_payment=post.date,
                                                     balance=F('balance') - post.amount,
                                                     status=Case(
                                                         When(balance=post.amount, then=Value("Paid")),
                                                         default=Value("Unpaid")
                                                     ))

【讨论】:

  • 这成功了!!!谢谢,如果我有足够的回购,我会赞成这个答案。 :D
【解决方案2】:

为了让它工作,我们需要把字段以外的所有东西都放在'='的右边

status=Case(
    When(balance=post.amount, then=Value("Paid")),
    default=Value("Unpaid")),
)

你也可以这样做

status=Case(
    When(balance__gt=post.amount, then=Value("Paid")), 
    When(balance__lte=post.amount, then=Value("Unpaid")), 
)

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 2018-06-11
    • 2018-02-01
    • 2012-09-20
    • 2021-03-26
    • 1970-01-01
    • 2018-04-04
    • 2014-09-17
    • 1970-01-01
    相关资源
    最近更新 更多