[Fixed]-How to customise django inline admin form's delete function

2👍

You should define a custom Formset and override the delete behaviour. Here’s the code:

class ChildFormSet(BaseInlineFormSet):
    def delete_existing(self, obj, commit=True):
        obj.parent = None
        if commit:
            obj.save()


class ChildModelInline(admin.TabularInline):
    model = ChildModel
    formset = ChildFormSet


@admin.register(ParentModel)
class ParentModelAdmin(admin.ModelAdmin):
    inlines = [ChildModelInline,]

0👍

What all you can do is first define a custom action for the respective model and in that action just put the models.ForeignKey field to null.So it will remove the child parent relation between the objects but it won’t delete the object from the database.

0👍

It can be controlled from the on_delete attribute of model property. Lookinto the below line:

option = models.ForeignKey(ServiceOption, on_delete=models.CASCADE, related_name='additional')

Leave a comment