Светлячный Dev Лог

Tue 16 November 2010

Helper to use SELECT FOR UPDATE in Django

Tags: django, sql

ru

Just wrote a simple helper to use SELECT FOR UPDATE construction in django:

from django.db import DEFAULT_DB_ALIAS

def select_for_update(queryset):
    """ Returns query, rewrited to use SELECT ... FOR UPDATE.
        Can be used in transaction to get lock on selected rows.
        Database must support this SQL statements.

        Example:
        >>> query = select_for_update(MyModel.objects.filter(blah = 'minor'))
        >>> unicode(query.query)
        "SELECT * FROM myapp_mymodel WHERE blah = 'minor' FOR UPDATE"
    """
    sql, params = queryset.query.get_compiler(DEFAULT_DB_ALIAS).as_sql()
    return queryset.model._default_manager.raw(sql + ' FOR UPDATE', params)

Comments !