code: def get_sort_field(attr, model):
      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