code: def field_to_dict(field, instance):
      
      # avoid a circular import
      from django.db.models.fields.related import ManyToManyField
  
      return (many_to_many_field_to_dict(field, instance) if isinstance(field, ManyToManyField)
              else field.value_from_object(instance))
docs: Converts a model field to a dictionary