# ORM model 'save' method and race condition / ACID Isolation

**URL:** <https://forum.djangoproject.com/t/orm-model-save-method-and-race-condition-acid-isolation/32883>\
**Category:** Using the ORM\
**Created:** [July 11, 2024, 8:51am UTC](https://forum.djangoproject.com/t/orm-model-save-method-and-race-condition-acid-isolation/32883 "2024-07-11T08:51:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LiorA1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/liora1/32/4101_2.png) [@LiorA1](https://forum.djangoproject.com/u/LiorA1)\
**Post date:** [July 11, 2024, 8:51am UTC](https://forum.djangoproject.com/t/orm-model-save-method-and-race-condition-acid-isolation/32883/1 "2024-07-11T08:51:28Z")

</div>

Hi,

if we examine the following code:

```auto
class SomeModel(models.Model):
    ...
    def save(self, *args, **kwargs):
        self.validate_unique()
        super().save()

    def validate_unique(self, *args, **kwargs):
        super().validate_unique(*args, **kwargs)
        
        # Doing an ORM query on the model with *filter* on the model attributes & an associated model, 
        # and if exist - dont create a new instance, if not - create a new instance.

```

Now, it will work with the development server, but not with a production one like gunicorn or uvicorn, because the `save` method gurantee us atomicity, but not isolation, right?

for us to gurantee isolation, we should use ‘get\_or\_create’ on the view level?

I’m mistake somewhere?  
Any comment will help, Thanks!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 12, 2024, 2:46pm UTC](https://forum.djangoproject.com/t/orm-model-save-method-and-race-condition-acid-isolation/32883/2 "2024-07-12T14:46:00Z")

</div>

> [@LiorA1](#):
>
> Now, it will work with the development server,

Actually, this isn’t necessarily true either. The development server can be subject to the same race conditions as your production server. (You generally don’t see them, because you don’t often run concurrent requests on it - but it can happen.)

> [@LiorA1](#):
>
> for us to gurantee isolation, we should use ‘get\_or\_create’ on the view level?

Actually, I think you’re looking for [`update_or_create`](https://docs.djangoproject.com/en/5.0/ref/models/querysets/#update-or-create)?

Regarding isolation though, you have the option to [set the isolation level](https://docs.djangoproject.com/en/5.0/ref/databases/#isolation-level) in your PostgreSQL database connections.

Also see [Django DB Transactions](https://forum.djangoproject.com/t/django-db-transactions/24314)

---

<div class="post-metadata">

**Author:** ![LiorA1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/liora1/32/4101_2.png) [@LiorA1](https://forum.djangoproject.com/u/LiorA1)\
**Post date:** [July 13, 2024, 11:14am UTC](https://forum.djangoproject.com/t/orm-model-save-method-and-race-condition-acid-isolation/32883/3 "2024-07-13T11:14:04Z")

</div>

It might be dependant on the situation, if some item exist we dont want to modify it ,iirc, so ‘get\_or\_create’ is more suitable.

need to take another look.

Thanks!
