[Fixed]-How to use for loop variable inside if block

1👍

If you want to not apply the check when date_paid is 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:

GratuityPayment.objects.filter(date_paid__isnull=False, 
                               employment_number=employment_number)
👤alecxe

0👍

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.

Leave a comment