foo_package: ^1.2.3-beta
上面的代码结尾,我在 creator_payouts = self.filter(paid=False)
processed_user_ids = []
if not creator_payouts.exists(): // here I check the existence
return
for creator_payout in creator_payouts:
do_something()
creator_payouts.update(paid=True) // maybe this does something?
CreatorRecord.objects.filter(
user__in=processed_user_ids,
created_for__lte=creator_payouts[0].created_for // error here
).update(processed_for_payout=True)
处出现索引错误。
为什么会收到错误消息,尤其是通过上述creator_payouts[0]
条件排除了空列表情况
答案 0 :(得分:1)
好吧,当您更新creator_payouts
时,数据库就会被paid=True
更新。由于creator_payouts
是一个惰性查询集,当您调用creator_payouts[0]
对其求值时,它将获得空查询集。因此,您需要通过强制评估将初始值存储在某个位置。像这样:
if not creator_payouts.exists(): // here I check the existence
return
for creator_payout in creator_payouts:
# or store the first creator_payout in a variable here
do_something()
unpaid_creator_payouts_list = list(creator_payouts) # force evaluation
creator_payouts.update(paid=True)
CreatorRecord.objects.filter(
user__in=processed_user_ids,
created_for__lte=unpaid_creator_payouts_list[0].created_for // or use the first stored payout_creator in previous loop
).update(processed_for_payout=True)
或在CreatorRecord
过滤器之后运行更新操作。
答案 1 :(得分:0)
creator_payouts
是queryset。如果想先变得更好,请尝试使用creator_payouts.first()
而不是creator_payouts[0]
。
CreatorRecord.objects.filter(
user__in=processed_user_ids,
created_for__lte=creator_payouts.first().created_for
).update(processed_for_payout=True)