Wednesday, July 25, 2012

Re: Variable # of fields in a form

On Wednesday, 25 July 2012 08:02:14 UTC+1, Alex Strickland wrote:
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


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" .
--
DR.

--
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/-/sk1KvdyZMeQJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment