#NEWLINE# def label_for_field( name , model , model_admin = None , return_attr = False ) : #NEWLINE# #INDENT# '\n    Returns a sensible label for a field name. The name can be a callable or the\n    name of an object attributes, as well as a genuine fields. If return_attr is\n    True, the resolved attribute (which could be a callable) is also returned.\n    This will be None if (and only if) the name refers to a field.\n    ' #NEWLINE# attr = None #NEWLINE# try : #NEWLINE# #INDENT# field = model . _meta . get_field_by_name ( name ) [ 0 ] #NEWLINE# if isinstance ( field , RelatedObject ) : #NEWLINE# #INDENT# label = field . opts . verbose_name #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# label = field . verbose_name #NEWLINE# #UNINDENT# #UNINDENT# except models . FieldDoesNotExist : #NEWLINE# #INDENT# if ( name == '__unicode__' ) : #NEWLINE# #INDENT# label = force_text ( model . _meta . verbose_name ) #NEWLINE# attr = six . text_type #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( name == '__str__' ) : #NEWLINE# #INDENT# label = force_str ( model . _meta . verbose_name ) #NEWLINE# attr = bytes #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if callable ( name ) : #NEWLINE# #INDENT# attr = name #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( model_admin is not None ) and hasattr ( model_admin , name ) ) : #NEWLINE# #INDENT# attr = getattr ( model_admin , name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if hasattr ( model , name ) : #NEWLINE# #INDENT# attr = getattr ( model , name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# message = ( "Unable to lookup '%s' on %s" % ( name , model . _meta . object_name ) ) #NEWLINE# if model_admin : #NEWLINE# #INDENT# message += ( ' or %s' % ( model_admin . __class__ . __name__ , ) ) #NEWLINE# #UNINDENT# raise AttributeError ( message ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if hasattr ( attr , 'short_description' ) : #NEWLINE# #INDENT# label = attr . short_description #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if callable ( model_admin ) : #NEWLINE# #INDENT# if ( attr . __name__ == '<lambda>' ) : #NEWLINE# #INDENT# label = '--' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# label = pretty_name ( attr . __name__ ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# label = pretty_name ( name ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if return_attr : #NEWLINE# #INDENT# return ( label , attr ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return label