I am using DjangoREST with PostgreSQL. I have these two models: author/models.py:
class Author(models.Model):
name = models.CharField(max_length=256)
allsize = models.FloatField(default=0)
and in files/models.py:
class File(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
name = models.CharField(max_length=256)
size = models.FloatField(default=0)
If I upload multiple files having the same size (from the front end), in order to update the allsize field (which is sum of the all uploaded files) of the author, DjangoREST didn't update correctly the values maybe according to the concurrency (they have same size), despite I am doing a raw sql transaction:
with connection.cursor() as cursor:
cursor.execute("BEGIN; set transaction isolation level read committed;"
"update author_author set allsize = allsize + %s where author_id = %s;",
[validated_data.get('size', 0), validated_data.get('author', 0)])
cursor.execute("COMMIT;")