          if fields and f.name not in fields:
              continue
          if exclude and f.name in exclude:
              continue
          if isinstance(f, ManyToManyField):
              # If the object doesn't have a primary key yet, just use an empty
              # list for its m2m fields. Calling f.value_from_object will raise
              # an exception.
              if instance.pk is None:
                  data[f.name] = []
 
</s> remove     # avoid a circular import
    from django.db.models.fields.related import ManyToManyField
 </s> add  </s> remove             return isinstance(f, Field) and not isinstance(f, related.ManyToManyField)
 </s> add             return isinstance(f, Field) and not f.many_to_many </s> remove         # ForeignKey or ManyToManyFields
        if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
 </s> add         # Foreign key or many-to-many fields
        if db_field.many_to_many or isinstance(db_field, models.ForeignKey): </s> remove             if isinstance(f, models.ManyToManyField):
 </s> add             if f.many_to_many: </s> remove                 if (isinstance(field, models.ManyToManyField) and
                        not field.remote_field.through._meta.auto_created):
 </s> add                 if field.many_to_many and not field.remote_field.through._meta.auto_created:
        if f.many_to_many:
          if fields and f.name not in fields:
              continue
          if exclude and f.name in exclude:
              continue
        if f.many_to_many:
              # If the object doesn't have a primary key yet, just use an empty
              # list for its m2m fields. Calling f.value_from_object will raise
              # an exception.
              if instance.pk is None:
                  data[f.name] = []
keep keep keep keep replace keep keep keep keep keep
 Refs #24227 -- Replaced M2M isinstance checks by field.many_to_many

Thanks Markus Holtermann, Collin Anderson and Tim Graham for the reviews. 
django/forms/models.py