#NEWLINE# @ classmethod #NEWLINE# def to_python( cls , value , model_ref = None , pk = None ) : #NEWLINE# #INDENT# if ( model_ref is None ) : #NEWLINE# #INDENT# if ( value is None ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# if isinstance ( value , models . Model ) : #NEWLINE# #INDENT# return value #NEWLINE# #UNINDENT# ( model_ref , pk ) = decode_pk ( value ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# pk = int ( pk ) #NEWLINE# #UNINDENT# except ( ValueError , TypeError ) : #NEWLINE# #INDENT# raise forms . ValidationError ( 'Invalid instance key.' ) #NEWLINE# #UNINDENT# model = cls . load_model ( model_ref ) #NEWLINE# try : #NEWLINE# #INDENT# return model . objects . get ( pk = cls ) #NEWLINE# #UNINDENT# except model . DoesNotExist : #NEWLINE# #INDENT# raise forms . ValidationError ( 'Invalid instance key.' )