Hi, please take a look at the following code.

def create_foo_form(baz):
    class FooForm(forms.ModelForm):
       def __init__(self, *args, **kwargs):
           print "Initializing a FooForm object"
           # Init stuff here
    return FooForm

class FooInline(admin.TabularInline):
    model = Foo

    def get_formset(self, request, obj=None, **kwargs):
        if obj:
            kwargs['form'] = create_foo_form(obj)
        return super(FooInline, self).get_formset(request, obj,
**kwargs)

class BazAdmin(admin.ModelAdmin):
    inlines = [FooInline,]


Suppose we are changing a Baz object. First, get_formset method will
be passed the Baz object as obj and following that a FooForm class
will be created and passed to the superclass method.

Here's the part that's beyond my understanding: After that,
get_formset gets executed for an extra time with obj=None. Because obj
is None, the method doesn't pass FooForm to the superclass method, and
therefore the superclass is expected to construct the formset using
default form (self.form - which is not FooForm). But to my surprise I
am getting FooForm objects initialized.

So I have 2 questions:
1. Why does get_formset get called an extra time with obj=None?
2. Why are FooForm objects used when a formset is constructed in this
second time?

Regards.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to