[Fixed]-Django changing the db

1👍

Use .update(b=False) on the queryset:

@receiver(pre_save, sender=A)
def my_callable(sender, instance, **kwargs):
    A.objects.filter(b=True).update(b=False)

The update is done in SQL, doesn’t call model’s save() method or trigger any signals

.update() docs

0👍

Assuming you’re able to use Django1.8, this exact use case is covered in the docs actually: https://docs.djangoproject.com/en/1.8/ref/models/instances/#customizing-model-loading

Cliff notes: use the from_db method to customize loading of the model and save a copy of the instance’s attributes as it is loaded, and then compare them before it is saved.

If you want to compare form data to a model instance to see if a user is changing it, do that in the view, not with a signal.

Leave a comment