[Answered ]-Pythonic method for Django loop

1👍

Before anything else you can at least avoid repeating db lookups. For example do this:

identity = Identity.objects.get(pk=id)
birth_certificate = BirthCertificate.objects.get(pk=id)

Then apply your conditional logic on these objects. For example:

if birth_certificate.lastname == identity.lastname and birth_certificate.firstname == identity.firstname:rstname :
  # Do something , etc.
👤gipsy

1👍

You can use this query for checking if the same object exist in Identity table

person = BirthCertificate.objects.get(pk=id)
try :
    identity_object = Identity.objects.get(firstname=person.firstname, lastname=person.lastname, birthcity=person.birthcity, birthday=person.birthday)
    if Identity.objects.exclude(folderId__isnull=True):
        BirthCertificate.objects.filter(pk=id).update(folderId=identity_object.folderId)
    else : 
        return HttpResponseRedirect(reverse('home'))
except Identity.DoesNotExist:
    HttpResponseRedirect(reverse('home'))

Leave a comment