Josh Smeaton
Josh Smeaton

Reputation: 48730

Generating and submitting a dynamic number of objects in a form with Django

I want to be able to update a dynamic number of objects within a single form using Django and I'm wondering what the best way to do this would be. An example of a similar situation may help.

Model:

class Customer(Model.models):
    name = models.CharField(max_length=100)
    active = models.BooleanField()

Form (I know I'm mixing view and template code here which doesn't work but this is a general idea for what the form is supposed to do):

customers = Customer.objects.all()
for c in customers:
    print <li> {{ c.name }} <input type="checkbox" value="{{ c.active }}" name="?" />

How would I go about submitting a list of these objects? Would the best bet be to attach the id of the customer into each 'row' and then process based on the id? Is there a mechanism for submitting a list of tuples? What would be the ideal solution?

Upvotes: 2

Views: 412

Answers (1)

Van Gale
Van Gale

Reputation: 43932

Formsets!

Also, the equivalent for forms generated directly models are model formsets.

Upvotes: 8

Related Questions