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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:54:05+00:00 2026-06-08T21:54:05+00:00

[ edit ] Since I have removed the new_method = … and syspar.method.add() lines

  • 0

[edit]

Since I have removed the new_method = … and syspar.method.add() lines (this is taken care of by method.save()), the solution that I originally discarded has become DRY, so it is perfectly acceptable.

I do still think it strange that setting self.fields[].initial (as per pyriku’s answer) only works if extra > 0, because the simple use case where you don’t want a form displayed until an ‘add form’ button is clicked, must have extra=0 and it should be possible to set initial values for the newly added form.

[/edit]

Disclaimer:
I have looked at many answers to related questions, and I have only found one solution that works, but appears crufty to me and not very DRY (4378667) /Disclaimer

I have a modelformset where I set extra to 0, because I only want to display a new form when the user presses “add_method”. Everything works fine, except that the newly added forms are empty (desired behaviour: they contain the default value for name that I set in the Method table)

In models.py:

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

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

In view.py:

syspar = System.objects.get(id=sys_id)
MethodFormSet = modelformset_factory(Method, form=MethodForm, extra=0)
if request.POST:
    if 'add_method' in request.POST:
        new_method = Method.objects.create(name='whatever')
        syspar.method.add(new_method)

        post = request.POST.copy()
        post['method-TOTAL_FORMS'] = repr(int(post['method-TOTAL_FORMS'])+ 1)

        post['method-2-name'] = 'whatever' # code needed here to get actual value of -2-

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

I have a problem with the line: post[‘method-2-name’]=”
This is awful, because:

  • I have already set the value in the db table
  • I will need extra cruft to determine the value of ‘2’ for each case

I tried this approach, also tried setting initial (as suggested in answers to some other related questions), but neither worked and both also resulted in non-DRY code.

There must be a better way to do this…

  • 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-08T21:54:06+00:00Added an answer on June 8, 2026 at 9:54 pm

    Probably you will need to override the __init__() method of that form for setting the initial values that you want. Something like:

    def __init__(self, *args, **kwargs):
        super(MethodForm, self).__init__(*args, **kwargs)
        self.initial['field'] = 'value'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: I've learned much since the posting of this question, and so I have
Edit (2012-04-12): Since this question was asked it is now possible (as of jQuery
EDIT: This question is now redundant since Twitter no longer supports basic auth. I've
I have a bit of a problem. I'm trying to add,remove and edit items
I have this mapping ( it comes from adventureworks since I used it in
Edit: since I was appending via text the file was not being saved properly,
Edit Since there were many downvotes and people who didn't understand what I'm asking
EDIT : I completely re-wrote the question since it seems like I was not
I'm solving UVA's Edit Step Ladders on an uva sub-site named programming-challenges.com, but since
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question

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.