code: def get_model_field(model, field_name):
      
      meta = model._meta
      try:
          if DJANGO19:
              field = meta.get_field(field_name)
          else:
              field = meta.get_field_by_name(field_name)[0]
          return field
      except:
          if DJANGO19:
              related_objs = (
                  f for f in meta.get_fields()
                  if (f.one_to_many or f.one_to_one)
                  and f.auto_created and not f.concrete
              )
              related_m2m_objs = (
                  f for f in meta.get_fields(include_hidden=True)
                  if f.many_to_many and f.auto_created
              )
          else:
              related_objs = meta.get_all_related_objects()
              related_m2m_objs = meta.get_all_related_many_to_many_objects()
  
          related_objects = {
              o.get_accessor_name(): o
              for o in chain(related_objs, related_m2m_objs)
          }
          if field_name in related_objects:
              return related_objects[field_name]
          else:
              # check virtual fields (1.7)
              if hasattr(meta, 'virtual_fields'):
                  for field in meta.virtual_fields:
                      if field.name == field_name:
                          return field
  
              raise AttributeError(
                  '%s is not a valid field for %s' % (field_name, model)
              )
docs: Return a field given a model and field name.
  
      Arguments:
          model: a Django model
          field_name: the name of a field
  
      Returns:
          A Django field if `field_name` is a valid field for `model`,
              None otherwise.