Login

SELECT FOR UPDATE in Django < 1.4

Author:
joaofrancese
Posted:
June 13, 2012
Language:
Python
Version:
1.2
Tags:
queryset
Score:
0 (after 0 ratings)

SELECT FOR UPDATE, which does row-level locking in the database, was added by Django only in version 1.4. This snippet emulates that feature in older versions of Django. Tested in Django 1.2, but should work in newer versions as well.

select_related is removed because it causes errors when used with RawQuerySet.

1
2
3
4
5
6
def select_for_update(queryset):
    """Returns a RawQuerySet with SELECT ... FOR UPDATE."""
    query = queryset.query.clone()
    query.select_related = False
    sql = "%s FOR UPDATE" % query
    return queryset.model.objects.raw(sql)

More like this

  1. MultiSelectField with comma separated values (Field + FormField) by quinode 3 years ago
  2. CompressedTextField for Django 1.4+ by devhulu 2 years, 3 months ago
  3. Django Admin Speedup for big tables on postgres by potiuk 2 years, 5 months ago
  4. Link raw_id_fields (both ForeignKeys and ManyToManyFields) to their change pages by EmilStenstrom 4 years, 7 months ago
  5. Digg-like paginator, updated by miracle2k 7 years ago

Comments

Please login first before commenting.