Django filter and update atomicity

Viewed 183

When updating filtered records, is there a need for select_for_update if you want to make sure that the filtered rows won't get changed by some other process before this update is done, or is this filter + update atomicity guaranteed out-of-the-box?

I.e., is this:

with transaction.atomic():
    Foo.objects.select_for_update().filter(id=1).update(**kwargs)

equivalent to:

Foo.objects.filter(id=1).update(**kwargs)

?

I'm using Django 1.6.x.

1 Answers

Short answer:

You don't need to use the select_for_update when everything makes in a single query (your case).

Longer answer:

It calls "Dirty reads" and it is covered by the "Read Committed" transaction isolation level which is usually used by default.

Related