gpt4 book ai didi

python - 我如何将两个字段的总和与 Django 中同一模型上的另一个字段进行比较

转载 作者:行者123 更新时间:2023-11-29 12:04:24 25 4
gpt4 key购买 nike

这是我的模型

class Wallet(models.Model):
"""
Keep track of the monetary values of a company's wallet
"""
serializer_class = WalletSocketSerializer

company = models.OneToOneField(Company, verbose_name=_('company'))
packaged_credits = models.BigIntegerField(_('packaged credits'), default=0)
purchased_credits = models.BigIntegerField(_('purchased credits'), default=0)
low_credits_threshold = models.BigIntegerField(default=0)

现在我想在总积分低于阈值时发送警报,这相当于在此 SQL 中获取所有低钱包

select * from wallets_wallet where (packaged_credits + purchased_credits) < low_credits_threshold;

我想知道如何在 Django 中执行它,现在我已经尝试了以下方法,它有效,但我认为它应该以更 Django 的方式完成:

low_wallets = []
for wallet in Wallet.objects.all():
if wallet.packaged_credits + wallet.purchased_credits < wallet.low_credits_threshold:
low_wallets.append(wallet)

最佳答案

from django.db.models import F

low_wallets = Wallet.objects.filter(
low_credits_threshold__gt=F('packaged_credits')+F('purchased_credits')
)

关于python - 我如何将两个字段的总和与 Django 中同一模型上的另一个字段进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35057502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com