2

The Django documentation says that "As shown above you can define the number of extra forms. What this means is that you are telling the formset how many additional forms to show in addition to the number of forms it generates from the initial data." Source here.

However, (at least) with inline formsets, that doesn't seem to be the case. It looks like the extra parameter determines the total number of forms. If I write extra=0 I'll simply have no forms at all to show, with extra=1 I'll only get the first one, and so on. Seems like you have to set extra to the length of the initial data (not very DRY, is it?)

Some examples where people do the same thing (extra=len(myinitial)):

The question is: Am I missing anything in the documentation or why does this make sense?

Note. I'm not including source code because I'm developing with django-extra-views with adds additional complexity to the sample code. Nonetheless, the few examples around seem to point to the same thing. (I'm not sure what I did with previous projects, though, but I think that I encountered the same problem with plain inline form sets).

Community
  • 1
  • 1
jleeothon
  • 2,907
  • 4
  • 19
  • 35

2 Answers2

2

From Django documentation about model formsets (and inline formsets):

As with regular formsets, it’s possible to specify initial data for forms in the formset by specifying an initial parameter when instantiating the model formset class returned by modelformset_factory. However, with model formsets, the initial values only apply to extra forms, those that aren’t bound to an existing object instance.

I was noticing the same behavior, at least for Django 1.4 and using model formsets. I think the documentation needs to be improved.

Also, I found this related bug here.

m0y
  • 137
  • 2
  • 9
1

extra is useful in this construct (to be put in admin.py )

from django.contrib import admin
from guardian.models import UserObjectPermission

class InlineUserObjectPermission(admin.TabularInline):
    model = UserObjectPermission
    extra = 0

_inlines_ = [  InlineUserObjectPermission ]

class MyUserAdmin(UserAdmin):
    model = MyUser
    inlines = _inlines_

admin.site.register(MyUser, MyUserAdmin)

With this, for any MyUser page in the Django admin pages, you will see at the bottom of the page the UserObjectPermission associated to that user, plus a number of extra empty lines, to be compiled if you wish to; extra controls how empty many extra lines there will be.

am70
  • 591
  • 6
  • 8