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
- FileField having auto upload_to path by junaidmgithub 1 month ago
- LazyPrimaryKeyRelatedField by LLyaudet 1 month, 1 week ago
- CacheInDictManager by LLyaudet 1 month, 1 week ago
- MYSQL Full Text Expression by Bidaya0 1 month, 1 week ago
- Custom model manager chaining (Python 3 re-write) by Spotted1270 1 month, 2 weeks ago