How to set initial values for new form in django modelformset
Disclaimer:
I have looked at many answers to related questions, but I have only found one solution that works, but appears crufty to me and not very DRY ([4378667][1]) /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 (expected 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][2], 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]: http://stackoverflow.com/questions/4378667
  [2]: http://stackoverflow.com/a/8223856/1406825
How to set initial values for new form in django modelformset
===

Disclaimer:
I have looked at many answers to related questions, but I have only found one solution that works, but appears crufty to me and not very DRY ([4378667][1]) /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 (expected 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][2], 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]: http://stackoverflow.com/questions/4378667
  [2]: http://stackoverflow.com/a/8223856/1406825