views:

97

answers:

1

Sorry for a newbie question but...

Can someone shed some light on what is the use case for inlineformset_factory?

I have followed example from Django documentation:

#Models
class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=100)

#View

def jojo(request):

    BookFormSet = inlineformset_factory(Author, Book)
    author = Author.objects.get(name=u'Mike Royko')
    formset = BookFormSet(instance=author)


    return render_to_response('jojo.html', {
        'formset': formset,
    })

#jojo.html
<form action="" method="POST">
<table>

{{ formset }}

</table>
<input type="submit" value="Submit" />
</form>

But it only displays book fields.

My understanding was that formset would display Book form with inline Author form just like Django Admin. On top of that I can't easily pass initial values to formset?

Then how is it better then using two separate AuthorForm and BookForm?

Or am i missing something obvious?

+1  A: 

The beauty of the inlineformset_factory (and modelformset_factory) is the ability to create multiple model instances from a single form. If you were to simply 'use two separate forms' the id's of the form's fields would trample each other.

The formset_factory functions know how many extra form(sets) you need (via the extra argument) and sets the id's of the fields accordingly.

czarchaic
So if i understood correctly inlineformset_factory just "scales" better. But still I can specify prefix in "regular" forms to prevent id clashing
Mike