nodes: 0, module; 1, function_definition; 2, function_name:get_sort_field; 3, parameters; 4, block; 5, identifier:attr; 6, identifier:model; 7, expression_statement; 8, try_statement; 9, comment:"""
      Get's the field to sort on for the given
      attr.
  
      Currently returns attr if it is a field on
      the given model.
  
      If the models has an attribute matching that name
      and that value has an attribute 'sort_field' than
      that value is used.
  
      TODO: Provide a way to sort based on a non field
      attribute.
      """; 10, block; 11, except_clause; 12, if_statement; 13, identifier:FieldDoesNotExist; 14, block; 15, call; 16, block; 17, if_statement; 18, return_statement; 19, attribute; 20, argument_list; 21, return_statement; 22, call; 23, block; 24, None; 25, attribute; 26, identifier:get_field; 27, identifier:attr; 28, identifier:attr; 29, identifier:isinstance; 30, argument_list; 31, expression_statement; 32, if_statement; 33, identifier:model; 34, identifier:_meta; 35, identifier:attr; 36, identifier:basestring; 37, assignment; 38, boolean_operator; 39, block; 40, identifier:val; 41, call; 42, identifier:val; 43, call; 44, return_statement; 45, identifier:getattr; 46, argument_list; 47, identifier:hasattr; 48, argument_list; 49, attribute; 50, identifier:model; 51, identifier:attr; 52, None; 53, identifier:val; 54, string; 55, call; 56, identifier:sort_field; 57, string_content:sort_field; 58, identifier:getattr; 59, argument_list; 60, identifier:model; 61, identifier:attr
code: def get_sort_field(attr, model):
      """
      Get's the field to sort on for the given
      attr.
  
      Currently returns attr if it is a field on
      the given model.
  
      If the models has an attribute matching that name
      and that value has an attribute 'sort_field' than
      that value is used.
  
      TODO: Provide a way to sort based on a non field
      attribute.
      """
  
      try:
          if model._meta.get_field(attr):
              return attr
      except FieldDoesNotExist:
          if isinstance(attr, basestring):
              val = getattr(model, attr, None)
              if val and hasattr(val, 'sort_field'):
                  return getattr(model, attr).sort_field
          return None