Multi object form

36 views
Skip to first unread message

David

unread,
Apr 24, 2012, 9:47:52 AM4/24/12
to django...@googlegroups.com
Hello

I need to produce a form that will contain a series of rows. Each row will be a specific object. Columns for each row will have data that will need editting.

ie:

Person1  EditA  EditB  EditC
Person2  EditA  EditB  EditC
Person3  EditA  EditB  EditC
Person4  EditA  EditB  EditC

The form should be submitted by one submit button that then updates each object.

Is it possible to produce this sort of form through any builtin Django functionality? From my (limited) understanding through reading all the docs I can find, this isn't something that can be achieved by a modelform or formsets.

Thank you for any assistance

Marcin Tustin

unread,
Apr 24, 2012, 9:49:47 AM4/24/12
to django...@googlegroups.com
Create a modelform, then use a formset to manage all of the modelforms.

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



--
Marcin Tustin
Tel: 07773 787 105

Tom Evans

unread,
Apr 24, 2012, 9:52:28 AM4/24/12
to django...@googlegroups.com

This is exactly what formsets are used for. Each row in your '<form>'
corresponds to a django form, and the entire '<form>' is modelled by a
django formset.

Cheers

Tom

David

unread,
Apr 24, 2012, 10:02:12 AM4/24/12
to django...@googlegroups.com
 Thank you both for your replies.

Having created a modelform and used:

ArticleFormSet = formset_factory(ArticleForm, extra=2)

I have a queryset that produces the objects I want to edit in my formset. How can I bind these objects to the formset and therefore remove the need for the extra=2 parameter?


Tom Evans

unread,
Apr 24, 2012, 10:05:38 AM4/24/12
to django...@googlegroups.com

Use modelformset_factory instead. The FormSet class produced as a
result will accept a queryset argument, which will be used to work out
which objects are to be edited.

See:

https://docs.djangoproject.com/en/1.4/topics/forms/modelforms/#model-formsets

Cheers

Tom

David

unread,
Apr 24, 2012, 10:09:58 AM4/24/12
to django...@googlegroups.com
Hi

That is brilliant thank you! I've been struggling with this for too long :(

    MyModel Set = modelformset_factory(MyModel)
    formset =  MyModelFormSet(queryset= MyModel.objects.select_related().filter(left_team__isnull=True).filter(team=pk))

    variables = RequestContext(request, {
        'form': formset,
    })

Seems to do the job, except it adds an extra form at the bottom which I guess is for additions. Is there a way to remove that? so it is restricted solely to the results from my queryset?

David

unread,
Apr 24, 2012, 10:11:26 AM4/24/12
to django...@googlegroups.com
Adding ,extra=0 to the modelformset_factory() line appears to do the job.

Thanks for your help guys

Marcin Tustin

unread,
Apr 24, 2012, 10:12:04 AM4/24/12
to django...@googlegroups.com
There is documentation on the various options. It is possible to specify no extras.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/-zXhy_qXJcgJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages