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

  • Home
  • SEARCH
  • 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 8408445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:38:01+00:00 2026-06-09T23:38:01+00:00

I have a System that can have one or more Models. I have modeled

  • 0

I have a System that can have one or more Models. I have modeled this relationship in the database with a manytomany field. The code below is for editing the system and its associated methods in a single form.

Adding a new method by filling out its form and pressing submit works only the first time. If I then make a small change and submit again, I get the following message (generated by the code below):

METHODFORMSET.ERRORS: [{}, {'name': [u'Method with this Name already exists.']}]

This is caused by the fact that the name field is unique, but it should have updated, not created a new record, even though I am using the POST data to generate the methodformset instance…

Note that this behaviour only applies to the last appended method instance, not to ones that were already present in the table.

Here is the relevant code, can anyone let me know what I am doing wrong?

def sysedit(request, sys_id):

    system = System.objects.get(id=sys_id)
    MethodFormSet = modelformset_factory(Method, form=MethodForm)

    post = None
    if request.POST:
        post = request.POST.copy()
        if 'add_method' in request.POST:
            post['method-TOTAL_FORMS'] = repr(int(
                                                post['method-TOTAL_FORMS'])+ 1)

    systemform = SystemForm(data=post, instance=system)

    methodformset = MethodFormSet(data=post, prefix='method',
            queryset=Method.objects.filter(id__in=system.method.all()))

    if methodformset.is_valid():
        mfs = methodformset.save()
        print 'SAVED-method', mfs
        for mf in mfs:
            if systemform.is_valid():
                sp = systemform.save(mf)
                print 'SYSTEM', sp
            else:
                print 'SYSFORMSET.ERRORS:', systemform.errors
    else:
        print 'METHODFORMSET.ERRORS:', methodformset.errors

    return render_to_response('sysedit.html', 
            {'systemform': systemform, 
            'methodformset': methodformset, 
            'system': system},
            context_instance=RequestContext(request))


class System(models.Model):
    method = models.ManyToManyField(Method)
    ...

class Method(models.Model):
    name = models.CharField(unique=True)
    ...

class MethodForm(ModelForm):
    class Meta:
        model = Method

class SystemForm(ModelForm):
    def save(self, new_method=None, commit=True, *args, **kwargs):
        m = super(SystemForm, self).save(commit=False, *args, **kwargs)
        if new_method:
            m.method.add(new_method)
        if commit:
            m.save()
        return m

    class Meta:
        model = System
        exclude = ('method')

[EDIT after Sergzach’s answer]:

The problem is not how to deal with the Method with this name already exists error, but to prevent that from occurring in the first place. I think the actual problem may have something to do with the way modelformsets deal with new forms. Somehow it looks like it always tries to create a new instance for the last formset, regardless of whether it already exits.

So if I do not add a new formset after the last one was appended, the modelformset will try to re-create the last one (even though it was just created on the previous submit).

The initial situation is that I have 1 valid Method instance and 1 new unbound instance in the methodformset. I then fill out the form and hit save, which validates both Methods and binds the 2nd one, which is then saved to the table.
So far all is well, but if I then hit save the 2nd time the error occurs. Maybe this has to do with the fact that method-TOTAL_FORMS=2 and method-INITIAL_FORMS=1. Could it be that this causes modelformset to force a create on the 2nd Method?

Can anyone confirm/deny this?

[Edit after a weekend of not looking at the code]:

The problem is caused by the fact that I am saving the forms in the view and after saving, I am sending the original methodformset instance (from before the save) to the template. The problem can be solved by re-instantiating modelformset after the save, using the queryset and NOT the POST data.

So the general rule to prevent errors like this, is either to go to a different page after a save (avoid it altogether), or use the above solution.

Before I post this as THE solution, I need to do more testing.

  • 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-09T23:38:03+00:00Added an answer on June 9, 2026 at 11:38 pm

    I have solved the problem by re-instantiating modelformset after the save (see edit at the bottom of the question)

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

Sidebar

Related Questions

To have a general-purpose documentation system that can extract inline documentation of multiple languages,
I have a system service running on my Windows machine that can impersonate the
I have a category system that is related many-to-many with posts. How can I
I'm looking content management system that would have features similar to stackoverflow: Users can
The Application class in System.Windows.Forms have a some properties that can be quite useful.
I have a system whereby a user can view categories that they've subscribed to
Can the calendar control be customsised so that, say you have a system of
I have a simple application with a customer and appointment models, system admin can
Given a table of models 'A', that can have multiple child models 'B', of
I currently have a system in which one rails 2.3.2 has a database with

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.