Changeset 1830

Show
Ignore:
Timestamp:
01/17/12 10:01:42 (4 months ago)
Author:
vic
Message:

LockForUpdate?: add native lock call for django >=1.4

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • DjHDGutils/trunk/dbutils.py

    r1829 r1830  
    249249 
    250250class LockForUpate(object): 
    251  
    252     def lock_for_update(self): 
    253         """ 
    254         lock object for update 
    255         Return locked object 
    256  
    257         """ 
     251    def _raw_lock(self): 
    258252        cursor = connection.cursor() 
    259253        cursor.execute("""select id from \"%s\" where id=%%s for update""" % self._meta.db_table, 
     
    263257        transaction.set_dirty() 
    264258        return type(self).objects.get(pk=self.id) 
     259         
     260    def _native_lock(self): 
     261        return type(self).objects.select_for_update().get(pk=self.id) 
     262 
     263    def lock_for_update(self): 
     264        """ 
     265        lock object for update 
     266        Return locked object 
     267 
     268        """ 
     269        if hasattr(self._base_manager, 'select_for_update'): 
     270            return self._native_lock() 
     271 
     272        return self._raw_lock() 
    265273 
    266274