Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8064701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:27:03+00:00 2026-06-05T11:27:03+00:00

The scenario: I have models like this: class A(models.Model): … class B(models.Model): a =

  • 0

The scenario:

I have models like this:

class A(models.Model):
    ...

class B(models.Model):
    a = models.ForeignKey(A)
    ...

In the admin I have something like this:

class AInlinde(admin.StackedInline):
    ...
    model = A
    ...

class BAdmin(admin.ModelAdmin):
     ...
     inlines = [AInline]
     ...

Now I need to record in a log when a user deletes some model in the inline, but I can’t follow the code, until now, I know Badmin have a method: save_formset, which is this:

def save_formset(self, request, form, formset, change):
    """
    Given an inline formset save it to the database.
    """
    formset.save()

but I think that formset is generated through the method modelformset_factory, so I can’t know when I can override the delete behavior of that formset

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:27:05+00:00Added an answer on June 5, 2026 at 11:27 am

    The best way for override how the formset saves or deletes the objects, is make a class that inherits from BaseInlineFormSet and override the methods save and save_existing_objects:

    class NoDeleteFormset(BaseInlineFormSet):
        def save(self, commit=True, request=None):
            """Saves model instances for every form, adding and changing instances
            as necessary, and returns the list of instances.
            """
            if not commit:
                self.saved_forms = []
                def save_m2m():
                    for form in self.saved_forms:
                        form.save_m2m()
                self.save_m2m = save_m2m
            return self.save_existing_objects(commit) + self.save_new_objects(commit)
    
        def save_existing_objects(self, commit=True, request=None):
            self.changed_objects = []
            self.deleted_objects = []
            if not self.initial_forms:
                return []
    
            saved_instances = []
            for form in self.initial_forms:
                pk_name = self._pk_field.name
                raw_pk_value = form._raw_value(pk_name)
    
                # clean() for different types of PK fields can sometimes return
                # the model instance, and sometimes the PK. Handle either.
                pk_value = form.fields[pk_name].clean(raw_pk_value)
                pk_value = getattr(pk_value, 'pk', pk_value)
    
                obj = self._existing_object(pk_value)
                if self.can_delete and self._should_delete_form(form):
                    self.deleted_objects.append(obj)
                    obj.deleted_at = datetime.now()
                    obj.save()
    
                    # log there your user deleting the object!
    
                    continue
                if form.has_changed():
                    self.changed_objects.append((obj, form.changed_data))
                    saved_instances.append(self.save_existing(form, obj, commit=commit))
                    if not commit:
                        self.saved_forms.append(form)
            return saved_instances
    

    Then construct a class than inherits from StackedInline:

    class MetaInline(StackedInline):
        formset = NoDeleteFormset
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this scenario, I have 2 or more models: class Store(models.Model): name = models.CharField(max_length
I have a model that looks something like this: public class SampleModel { public
I have a model class like this: namespace Models { public struct Localization {
I have the following scenario: in my models.py class FooBar(models.Model): description = models.CharField(max_length=20) in
Lets say I have a set of model classes like this: public class Person
I have an object model that looks like this (pseudo code): class Product {
I have this scenario well, i'll let the model explain. public class ScheduleMonthlyPerDayModel {
I have a model that looks like this: public interface IEntity { int Id
In RavenDB, I have a document that looks like this: public class SystemNotification {
If i have a ViewModel like this: public class SignupViewModel { [Required] [DisplayName(Email:)] public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.