[英]How to use for loop variable inside if block
for instance in GratuityPayment.objects.all():
print instance.date_paid
if (employment_number == instance.employment_number):
raise forms.ValidationError('This Employment Mumber was paid on ' + str(instance.date_paid) + '. You cannot proceed with this retirement')
print instance.date_paid in printing all the date_paid that are stored in the database but str(instance.date_paid) is given None. 在打印所有存储在数据库中但已为str(instance.date_paid)支付的date_paid时, print instance.date_paid被赋予None。
Am having the following line as my output 正在将以下行作为我的输出
This Employment Mumber was paid on None. 该雇佣木材是无薪的。 You cannot proceed with this retirement
您无法继续此退休
How can i use instance.date_paid inside the if statement. 我如何在if语句中使用instance.date_paid 。
If you want to not apply the check when date_paid
is None
: 如果不想在
date_paid
为None
时应用支票:
for instance in GratuityPayment.objects.all():
if instance.date_paid is not None and employment_number == instance.employment_number:
raise forms.ValidationError('This Employment Mumber was paid on ' + str(instance.date_paid) + '. You cannot proceed with this retirement')
But, why don't use .filter()
and get only relevant records in the first place: 但是,为什么不使用
.filter()
并首先只获取相关记录:
GratuityPayment.objects.filter(date_paid__isnull=False,
employment_number=employment_number)
It is actually working. 它实际上正在工作。 Its Given None because that particular Table row did have None in the table.
它的给定为无,因为该特定的表行在表中确实没有任何内容。
I just populated the date_paid and it returned the date instead of None. 我只是填充了date_paid,它返回了日期而不是None。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.