[Django]-Django filter and order by not being able to modify field?

2👍

The change in the first line of your code goes unsaved, and then you’re just looping through the queryset and triggering save on model instances with no changes.

You’ll need to call save using the model instance on which you mutated state:

obj = LabHours.objects.filter(used=False).order_by("endtime").first()
obj.used = True
obj.save()
👤wim

1👍

This happened because you didn’t save the value that you updated.

first = LabHours.objects.filter(used=False).order_by("endtime").first()
first.used = True
first.save()
for hours in LabHours.objects.all():
    hours.save()
    print hours.used
👤2ps

Leave a comment