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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:58:33+00:00 2026-05-29T07:58:33+00:00

I have a model w/ a manytomany relation to another model. They both have

  • 0

I have a model w/ a manytomany relation to another model. They both have forms associated with them, the latter has a formset.

 class Foo(models.Model):
   name = models.CharField(max_length=20)
   bars = models.ManyToManyField("Bar",blank=True,null=True)

 class Bar(models.Model):
   name = models.CharField(max_length=20)

 class FooForm(ModelForm):
   class Meta:
     model = Foo

 class BarForm(ModelForm):
   class Meta:
     model = Bar

 BarFormSet = modelformset_factory(Bar,form=BarForm,extra=2)

In my view/template the standard ManyToManyField widget is replaced with the formset. Thus, I have to manually associate the instances of Bar specified in that formset with Foo’s ManyToManyField. I am doing this in Foo’s clean method:

 def clean(self,*arg,**kwargs):
   cleaned_data = self.cleaned_data
   # barSubFormInstance is the BarSubForm that is displayed in my view
   if barFormSetInstance.is_valid(): 
     barInstances = barFormSetInstance.save()
     cleaned_data["bars"] = barInstances
   return cleaned_data

This almost works. The problem is that it sets Foo.bars to the set of changed forms within the formset. Thus if I add one bar to my foo, then reload the form and add a second bar, the foo winds up only having that second bar.

According to the Django documentation:

The save() method returns the instances that have been saved to the database. If a given instance’s data didn’t change in the bound data, the instance won’t be saved to the database and won’t be included in the return value…

So I understand why my code is failing. I just don’t know what to do about it. What can I pass to cleaned_data[“bars”] that will add the newly modified forms but not remove the existing ones?

Many thanks for your help.

  • 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-29T07:58:34+00:00Added an answer on May 29, 2026 at 7:58 am

    For what it’s worth, there was an error in the above comment. And some other details I left out.

    The appropriate code looks like this:

    def clean(self,*arg,**kwargs):
       cleaned_data = self.cleaned_data
       # barFormSetInstance is the BarFormSet that is displayed in my view
       # it's already been validated by the time this function is called
       barInstances = [form.save() for form in barFormSetInstance if form.cleaned_data]
       cleaned_data["bars"] = barInstances
       return cleaned_data
    

    I also had to ensure that “blank” and “null” were both set to True for the ManyToManyField. (I’m not sure why I had to do that instead of overriding that field’s is_valid() method to just return True).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model Foo which has field bar. The bar field should be unique,
I have a basic model which references both ForeignKeys and ManyToMany objects. In edit
I have two models in Django linked together by ManyToMany relation like this: class
I have two models, EqTD (EquivalentTextDescription) and location. EqTD has a manytomany relation to
I have an object with a ManyToMany relation with another object. In the Django
I have django models with a manytomany connection with a through class: class userProfile(models.Model):
I have a model with self-referencing ManyToMany field: class Suite(models.Model): suites = models.ManyToManyField(self, related_name=parents,
I have a ManyToMany field with a relationship model. I want a formset, filtered
I have model Article it has field title with some text that may contain
Lets say I have model inheritance set up in the way defined below. class

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.