django modelformset - one form per related table row

Posted by Toby on Stack Overflow See other posts from Stack Overflow or by Toby
Published on 2010-03-23T15:00:05Z Indexed on 2010/03/23 15:03 UTC
Read the original article Hit count: 863

Hello,

I have two models:

class Model1():
    name = CharField()
    url = CharField()

class Model2():
    model1 = ForeignKey(Model1)
    user = ForeignKey(User)
    zzz = CharField()

There are 5 rows for model1 in the database, these are fixed and will rarely change.

I need to display a formset for model2 that allows users to enter the zzz value, the formset must always show one form per row in the model1 table, the label for each form in the formset must be the name of the related model1.

If the user deletes a model2 in the formset the next time the page loads it will render an empty zzz value for that form and the user must be able to edit the previous zzz value - meaning it must be pre populated with all model2 rows associated with the user.

The idea is to print each row in the model1 table as a form instead of the user selecting the related model1 name in a select box.

I know its not that complicated, but I'm seriously stumped and keep going round in circles!!

Many thanks in advance.

Similar to http://stackoverflow.com/questions/298779/form-or-formset-to-handle-multiple-table-rows-in-django

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models