#NEWLINE# def get_or_create( self , **kwargs ) : #NEWLINE# #INDENT# '\n        Looks up an object with the given kwargs, creating one if necessary.\n        Returns a tuple of (object, created), where created is a boolean\n        specifying whether an object was created.\n        ' #NEWLINE# assert kwargs , 'get_or_create() must be passed at least one keyword argument' #NEWLINE# defaults = kwargs . pop ( 'defaults' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# lookup = kwargs . copy ( ) #NEWLINE# for f in self . model . _meta . fields : #NEWLINE# #INDENT# if ( f . attname in lookup ) : #NEWLINE# #INDENT# lookup [ f . name ] = lookup . pop ( v . attname ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# self . _for_write = True #NEWLINE# return ( self . get ( ** lookup ) , False ) #NEWLINE# #UNINDENT# except self . model . DoesNotExist : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# params = dict ( [ ( k , v ) for ( k , v ) in kwargs . items ( ) if ( '__' not in k ) ] ) #NEWLINE# params . update ( defaults ) #NEWLINE# obj = self . model ( ** params ) #NEWLINE# sid = transaction . savepoint ( using = self . db ) #NEWLINE# obj . save ( force_insert = True , using = self . db ) #NEWLINE# transaction . savepoint_commit ( sid , using = self . db ) #NEWLINE# return ( obj , True ) #NEWLINE# #UNINDENT# except IntegrityError as e : #NEWLINE# #INDENT# transaction . savepoint_rollback ( sid , using = self . db ) #NEWLINE# exc_info = sys . exc_info ( ) #NEWLINE# try : #NEWLINE# #INDENT# return ( self . get ( ** lookup ) , False ) #NEWLINE# #UNINDENT# except self . model . DoesNotExist : #NEWLINE# #INDENT# six . reraise ( * exc_info )