              dependencies.extend(self._get_dependecies_for_foreign_key(field))
          # You can't just add NOT NULL fields with no default or fields
          # which don't allow empty strings as default.
          preserve_default = True
          if (not field.null and not field.has_default() and
                  not isinstance(field, models.ManyToManyField) and
                  not (field.blank and field.empty_strings_allowed)):
              field = field.clone()
              field.default = self.questioner.ask_not_null_addition(field_name, model_name)
              preserve_default = False
          self.add_operation(
 
</s> remove                             not isinstance(new_field, models.ManyToManyField)):
 </s> add                             not new_field.many_to_many): </s> remove                 both_m2m = (
                    isinstance(old_field, models.ManyToManyField) and
                    isinstance(new_field, models.ManyToManyField)
                )
                neither_m2m = (
                    not isinstance(old_field, models.ManyToManyField) and
                    not isinstance(new_field, models.ManyToManyField)
                )
 </s> add                 both_m2m = old_field.many_to_many and new_field.many_to_many
                neither_m2m = not old_field.many_to_many and not new_field.many_to_many </s> remove         if isinstance(f, 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: </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):
        if (not field.null and not field.has_default() and not field.many_to_many and
              dependencies.extend(self._get_dependecies_for_foreign_key(field))
          # You can't just add NOT NULL fields with no default or fields
          # which don't allow empty strings as default.
          preserve_default = True
        if (not field.null and not field.has_default() and not field.many_to_many and
        if (not field.null and not field.has_default() and not field.many_to_many and
                  not (field.blank and field.empty_strings_allowed)):
              field = field.clone()
              field.default = self.questioner.ask_not_null_addition(field_name, model_name)
              preserve_default = False
          self.add_operation(
keep keep keep keep replace 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/db/migrations/autodetector.py