                  if both_m2m or neither_m2m:
                      # Either both fields are m2m or neither is
                      preserve_default = True
                      if (old_field.null and not new_field.null and not new_field.has_default() and
                              not isinstance(new_field, models.ManyToManyField)):
                          field = new_field.clone()
                          new_default = self.questioner.ask_not_null_alteration(field_name, model_name)
                          if new_default is not models.NOT_PROVIDED:
                              field.default = new_default
                              preserve_default = False
 
</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 (not field.null and not field.has_default() and
                not isinstance(field, models.ManyToManyField) and
 </s> add         if (not field.null and not field.has_default() and not field.many_to_many and </s> remove         if isinstance(f, ManyToManyField):
 </s> add         if f.many_to_many: </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
                            not new_field.many_to_many):
                  if both_m2m or neither_m2m:
                      # Either both fields are m2m or neither is
                      preserve_default = True
                      if (old_field.null and not new_field.null and not new_field.has_default() and
                            not new_field.many_to_many):
                          field = new_field.clone()
                          new_default = self.questioner.ask_not_null_alteration(field_name, model_name)
                          if new_default is not models.NOT_PROVIDED:
                              field.default = new_default
                              preserve_default = False
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/db/migrations/autodetector.py