views:

25

answers:

1

I have a model like below.

class Content(SimpleModel):
    title = models.CharField(max_length=255)
    body = models.TextField()
    slug = models.SlugField(max_length=50)    

    def __unicode__(self):
        return self.title


class MediumStuff(models.Model):
    meta_value = models.TextField()
    meta_key = models.SlugField('Field Name', max_length=50, blank=True)
    content = models.ForeignKey(Content)

    def __unicode__(self):
        return self.slug


class SmallStuff(models.Model):
    text = models.CharField(max_length=60, blank=True, null=True)
    content = models.ForeignKey(Content)

What I wanna do is to create formset for content that has inline forms for models of MediumStuff and SmallStuff using inlineformset_factory()

I referred to Django Documentation, but they have a example of how to work with single foreign key model.

ContentFormSet = inlineformset_factory(Content, [MediumStuff, SmallStuff])

nor

ContentFormSet = inlineformset_factory(Content, (MediumStuff, SmallStuff))

didn't work.

Since it is possible to add multiple inlines to admin, I believe this can be done :)

Do you have any suggestion / any resources or tips? Or possibly tell me where I should look at to see how admin handles multiple inlines?

A: 

Just create one inline for each related model:

MediumStuffInline = inlineformset_factory(Content, MediumStuff)

SmallStuffInline = inlineformset_factory(Content, SmallStuff)

Take a look how admin does. Each inline is handled by a subclass of InlineModelAdmin[1]. The inline itself is created on the get_formset() method [2].

Check out the documentation on how to use more that one formset in a view [3][4]

[1] http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L228

[2] http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L1243

[3] http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#using-an-inline-formset-in-a-view

[4] http://docs.djangoproject.com/en/1.2/topics/forms/formsets/#using-more-than-one-formset-in-a-view

Igor Sobreira
That is so helpful, I will try that and look into how django admin works!!! Thanks a lot Igor!
mnmly