def METHOD_NAME(self, request, obj=None, **kwargs):  # noqa: C901
    """
    Copied from the Django InlineModelAdmin but instead of the inlineformset_factory we use the
    non_related_inlineformset_factory.
    """
    if 'fields' in kwargs:
        fields = kwargs.pop('fields')
    else:
        fields = flatten_fieldsets(self.get_fieldsets(request, obj))
    exclude = [*(self.exclude or []), *self.get_readonly_fields(request, obj)]
    if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude:
        exclude.extend(self.form._meta.exclude)
    exclude = exclude or None
    can_delete = self.can_delete and self.has_delete_permission(request, obj)
    queryset = self.model.objects.none()
    if obj:
        queryset = self.get_form_queryset(obj)
    defaults = {
        'form': self.form,
        'formfield_callback': partial(self.formfield_for_dbfield, request=request),
        'formset': self.formset,
        'extra': self.get_extra(request, obj),
        'can_delete': can_delete,
        'can_order': False,
        'fields': fields,
        'min_num': self.get_min_num(request, obj),
        'max_num': self.get_max_num(request, obj),
        'exclude': exclude,
        'queryset': queryset,
        **kwargs,
    }
    base_model_form = defaults['form']
    can_change = self.has_change_permission(request, obj) if request else True
    can_add = self.has_add_permission(request, obj) if request else True
    class DeleteProtectedModelForm(base_model_form):
        def hand_clean_DELETE(self):
            """
            We don't validate the 'DELETE' field itself because on
            templates it's not rendered using the field information, but
            just using a generic "deletion_field" of the InlineModelAdmin.
            """
            if self.cleaned_data.get(DELETION_FIELD_NAME, False):
                using = router.db_for_write(self._meta.model)
                collector = NestedObjects(using=using)
                if self.instance._state.adding:
                    return
                collector.collect([self.instance])
                if collector.protected:
                    objs = []
                    for p in collector.protected:
                        objs.append(
                            # Translators: Model verbose name and instance representation,
                            # suitable to be an item in a list.
                            _('%(class_name)s %(instance)s') % {
                                'class_name': p._meta.verbose_name,
                                'instance': p}
                        )
                    params = {
                        'class_name': self._meta.model._meta.verbose_name,
                        'instance': self.instance,
                        'related_objects': get_text_list(objs, _('and')),
                    }
                    msg = _("Deleting %(class_name)s %(instance)s would require "
                            "deleting the following protected related objects: "
                            "%(related_objects)s")
                    raise ValidationError(msg, code='deleting_protected', params=params)
        def is_valid(self):
            result = super().is_valid()
            self.hand_clean_DELETE()
            return result
        def has_changed(self):
            # Protect against unauthorized edits.
            if not can_change and not self.instance._state.adding:
                return False
            if not can_add and self.instance._state.adding:
                return False
            return super().has_changed()
    defaults['form'] = DeleteProtectedModelForm
    if defaults['fields'] is None and not modelform_defines_fields(defaults['form']):
        defaults['fields'] = ALL_FIELDS
    return non_related_inlineformset_factory(self.model, save_new_instance=self.save_new_instance, **defaults)