[Answer]-How to check every single rows of Django database table for True or False values in a field and perform calculation based on it?

1👍

Something like this:

for route in LiveRoutes.objects.filter(calculated=False).iterator():
    # Do your calculation

    route.calculated = True
    route.save()

You have a complication in that you have calculated as a NullBooleanField. You probably want it as a normal boolean field with a default of False.

You also have a number of potential problems with database transactions depending on how this gets called and what other activity is happening at the same time.

An alternative is django-async. With that you would post a job to do the calculation which would happen some time later. See https://pypi.python.org/pypi/django-async/

👤KayEss

Leave a comment