这是我的模特
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)
答案 0 :(得分:2)
from django.db.models import F
low_wallets = Wallet.objects.filter(
low_credits_threshold__gt=F('packaged_credits')+F('purchased_credits')
)
答案 1 :(得分:1)
Wallet.objects.extra(where = [&#34; packaged_credits + buying_credits&lt; low_credits_threshold&#34;])