[Fixed]-Is this a safe way to increment and get the value of a counter in Django?

1👍

A general rule-of-thumb is that any sort of custom queries or customized query behavior should go into manager methods.
This makes sense in your case, because you can increment, save and return the version number at a very low level: during the actual query.

Thus, use a manager (object = MyManager()) and write a SQL query that increments the version number (UPDATE mytable SET version=(version+1) WHERE pk=pk, see here), and immediately return the incremented version of the model instance, before any other call can be executed.

See also Managers

👤Remi

Leave a comment