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 7036453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:25:33+00:00 2026-05-28T01:25:33+00:00

I have two related (via foreignkey relation) models and created admin model for parent

  • 0

I have two related (via foreignkey relation) models and created admin model for parent with inlines. In several cases (edited in admin by boolean field), i need to delete all previous objects (inlines) and create new ones. I’ve tried to make it with save_model, where i can get all current object’s properties and decide if i need to recreate (inline) objects.
models:

class Model1(models.Model):
    reformat = models.BooleanField(default=False)
    ...
class Model2(models.Model):
    model1 = ForeignKey(Model1, related_name='model2')
    ...

admin:

class Model2Inline(admin.TabularInline):
    model = Model2

class Model1Admin(admin.ModelAdmin):
    inlines = [Model2Inline]
    def save_model(self, request, obj, form, change):
        super(Model1Admin, self).save_model(request, obj, form, change)
        if obj.reformat:
            obj.model2.all().delete()
            # creating new objects
            ...
        obj.save()

But if i try to delete these objects in model_save method i get ValidationError.
Is there other possibilities to solve this problem?

  • 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-05-28T01:25:34+00:00Added an answer on May 28, 2026 at 1:25 am

    I came across this problem as well, and, after an hour or two of banging my head against the wall, solved it by overriding save_formset() on the admin object instead of save_model() and doing the required manipulation after calling the super class’s save_formset() method. Doing it this way means the models which you are deleting are still present when the formset is saved, so you don’t get the validation error.

    Here’s what it might look like in the example you gave:

    class Model2Inline(admin.TabularInline):
        model = Model2
    
    class Model1Admin(admin.ModelAdmin):
        inlines = [Model2Inline]
        def save_formset(self, request, form, formset, change):
            super(Model1Admin, self).save_formset(request, form, formset, change)
            if formset.model is Model2:
                obj = formset.instance
                if obj.reformat:
                    obj.model2.all().delete()
                    # creating new objects
                    ...
                obj.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
Let's say I have two classes, Parent and Child. These two are related via
I have two tables that are related via a mapping table: keywords titles I
I have two tables related: DataEntered and Model DataEntered -currentModel Model One DataEntered can
Given these two models: class Profile(models.Model): user = models.ForeignKey(User, unique=True, verbose_name=_('user')) about = models.TextField(_('about'),
I have two basic models, Story and Category: class Category(models.Model): title = models.CharField(max_length=50) slug
I have two tables, events and photos, which relate together via the 'Event_ID' column.
I have two related classes which share a common interface and are both stored
I have two related tables in my database: Page and Tag. One Page can
I have two related ComboBoxes ( continents, and countries ). When the continents ComboBox

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.