Variable # of fields in a form

424 views
Skip to first unread message

Alex Strickland

unread,
Jul 20, 2012, 3:41:04 AM7/20/12
to django...@googlegroups.com
Hi

I am generating a page with a variable list of categories (shown in a
Bootstrap accordion control) displayed as checkboxes. So a user may
select any or all of the checkboxes to categorise an article.

I did that just as a wireframe/demo. Trouble is I didn't think how that
might work with Django's form processing, as I now have a variable list
of form fields to deal with.

Can it work with Django forms? Or should I go on my own way and analyse
the POST data to try and capture the selections and save them.

I'm new at this so normal excuses for idiocy...
--
Regards
Alex

Daniel Roseman

unread,
Jul 20, 2012, 7:37:44 AM7/20/12
to django...@googlegroups.com
You don't want a variable number of fields, you just want a single MultipleChoiceField field using a CheckboxSelectMultiple widget. 

--
DR.

Alex Strickland

unread,
Jul 23, 2012, 8:29:49 AM7/23/12
to django...@googlegroups.com
On 2012/07/20 01:37 PM, Daniel Roseman wrote:

> You don't want a variable number of fields, you just want a single
> MultipleChoiceField field using a CheckboxSelectMultiple widget.
>
> https://docs.djangoproject.com/en/1.4/ref/forms/fields/#multiplechoicefield
> https://docs.djangoproject.com/en/1.4/ref/forms/widgets/#checkboxselectmultiple

Thank you Daniel. I have done this and see that it answers my question
(that HTML output is pretty ugly!). In Bootstrap I have used an
accordion so that the the categories are grouped:

Internal
x Evan
x Joe
External
x Alex

where x's are checkboxes and only one of Internal or External would be
expanded at any one time. Do you have a recommendation as to how break
up this CheckboxSelectMultiple widget into groups (also variable)?

I know I am digging my own grave...

--
Regards
Alex

Alex Strickland

unread,
Jul 23, 2012, 9:08:26 AM7/23/12
to django...@googlegroups.com
On 2012/07/23 02:29 PM, Alex Strickland wrote:

> Thank you Daniel. I have done this and see that it answers my question
> (that HTML output is pretty ugly!). In Bootstrap I have used an
> accordion so that the the categories are grouped:
>
> Internal
> x Evan
> x Joe
> External
> x Alex
>
> where x's are checkboxes and only one of Internal or External would be
> expanded at any one time. Do you have a recommendation as to how break
> up this CheckboxSelectMultiple widget into groups (also variable)?

https://docs.djangoproject.com/en/dev/topics/forms/formsets/ ?

--
Regards
Alex

Alex Strickland

unread,
Jul 25, 2012, 3:02:14 AM7/25/12
to django...@googlegroups.com
On 2012/07/23 03:08 PM, Alex Strickland wrote:

> https://docs.djangoproject.com/en/dev/topics/forms/formsets/ ?

No.

This is my html that shows a nice looking control using bootstrap, with
lists of users broken up into their groups:

<div class="accordion" id="group_accordion">
{% for group in group_list %}
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse"
data-parent="#group_accordion" href="#{{ group.name }}">
{{ group.name }}
</a>
</div>
<div id="{{ group.name }}" class="accordion-body collapse {% if
forloop.counter0 == 0 %} in{% endif %}">
<div class="accordion-inner">
{% for user in group.user_set.all %}
<label class="checkbox"> <input type="checkbox" name="{{
user.id }}" > {{ user.first_name }} {{ user.last_name }}</label>
{% endfor %}
</div>
</div>
</div>
{% endfor %}
</div>

But for the life of me I cannot figure out the "Django" way of achieving
this and leveraging the benefits of something like this:

foo = forms.ModelMultipleChoiceField(
User.objects.all(),
widget=forms.CheckboxSelectMultiple)

I think I'll have to do all the messy data handling myself, but maybe
someone has the answer?

--
Regards
Alex

Daniel Roseman

unread,
Jul 25, 2012, 6:41:14 AM7/25/12
to django...@googlegroups.com
You could probably subclass the CheckboxSelectMultiple widget and override the `render` method to get what you want.

See the original code here:
unfortunately you'll need to copy-and-paste most of that code but that should give you the output you need while still leaving Django to do the "messy data handling" .
--
DR.

Doug Ballance

unread,
Jul 25, 2012, 8:18:06 PM7/25/12
to Django users
It seems to me that two fields might be the simple solution?

class SomeForm(Form):
internal = forms.ModelMultipleChoiceField(
<get internal qs>,
widget=forms.CheckboxSelectMultiple)
externa = forms.ModelMultipleChoiceField(
<get external qs>,
widget=forms.CheckboxSelectMultiple)


Then you can just output them in your layout {{form.internal}} and
{{form.external}}

Another thing that might be of interest to you is crispy forms:
https://github.com/maraujop/django-crispy-forms/

Alex Strickland

unread,
Jul 30, 2012, 3:42:20 AM7/30/12
to django...@googlegroups.com
Hi Doug

> Then you can just output them in your layout {{form.internal}} and
> {{form.external}}

Thought I'd better reply as while I work out what I am doing I probably
look rather rude. Internal/external was just an example, there may be
many more groups.

> Another thing that might be of interest to you is crispy forms:
> https://github.com/maraujop/django-crispy-forms/

Yes, it looks nice, but I am limiting myself to one bewildering new
technology at a time! They do seem to have some nice support for
layouts, but I'll just hard code that for the moment.

--
Regards
Alex

Alex Strickland

unread,
Jul 30, 2012, 4:06:02 AM7/30/12
to django...@googlegroups.com
On 2012/07/25 12:41 PM, Daniel Roseman wrote:

> You could probably subclass the CheckboxSelectMultiple widget and
> override the `render` method to get what you want.
>
> See the original code here:
> https://github.com/django/django/blob/master/django/forms/widgets.py#L749
> unfortunately you'll need to copy-and-paste most of that code but that
> should give you the output you need while still leaving Django to do the
> "messy data handling" .

Thanks Daniel. I am looking at that code distributed with 1.4, amazing
how different it is in the head version already.

I think I'll try and do something similar to Select() which caters for
<optgroup label="%s">, and if I can make that work then I'll override
the render method to suit my layout purposes (try, being the operative
word).

If I can make it work nicely it might be nice to upgrade it to the same
style as the head version and offer it back as a snippet - that looks a
long way off though!

--
Regards
Alex

Tomas Neme

unread,
Jul 30, 2012, 2:27:05 PM7/30/12
to django...@googlegroups.com
maybe this will help?
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> 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.
>



--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.
Reply all
Reply to author
Forward
0 new messages