yodebu
yodebu

Reputation: 167

Any specific reasons why one should use `update_or_create` in Django?

So I have been using Django models' update_or_create function to upsert rows in a model. Wanted to ask, except the transaction being atomic, are there any added benefits? We can always do


a = Models.objects.get(id=21)
a.some_field = 'new_value'
a.save()

Upvotes: 0

Views: 133

Answers (1)

MrFrenzoid
MrFrenzoid

Reputation: 1326

This is used generally to have less code, making it shorter.

For example, imagine a website with a "Register account" and later an "Edit account" methods.

You can either have different logic for each method or have the same logic for both and use update_or_create.

This way you can shorten your code, by avoiding having similar logic in different parts of your code.

Upvotes: 2

Related Questions