I need guidance in selecting multiple initial values for checkbox which are rendered using widget-tweaks.
My code is working great when I have a single initial value. However when there is more than 1 initial value, only the first will be checked in the checkbox.
forms.py
class GroupForm(forms.ModelForm):
class Meta:
model = Group
fields = ('name', 'category', 'pods')
def __init__(self, group_id, category_id, *args, **kwargs):
super(GroupForm, self).__init__(*args, **kwargs)
selected_group = Group.objects.get(id=group_id)
self.fields['name'].initial = selected_group.name
self.fields['category'].initial = selected_group.category.id
self.fields['pods'].queryset = Pod.objects.filter(category=selected_group.category.id)
associated_pods = selected_group.pod.all()
self.fields['pods'].initial = [pod.id for pod in associated_pods]
views.py
def GroupUpdate(request,id):
group = get_object_or_404(Group, pk=id)
context = {}
group_id = group.id
category_id = request.GET.get('category')
context['form'] = GroupForm(group_id, category_id)
return render(request, 'catalog/group_detail.html', context)
html file
...
<div class="form-group">
<label class="col-md-3 control-label">Group's Members</label>
<div class="col-md-6">
<div class="input-group btn-group">
<span class="input-group-addon">
<i class="fa fa-th-list"></i>
</span>
{% render_field form.pods|attr:"multiple:multiple"|attr:"data-plugin-multiselect" title="group_member" class="form-control" %}
</div>
</div>
</div>
...
I need the pre-selected initial values of the checkbox to be rendered dynamically. Currently self.fields['pods'].initial = [pod.id for pod in associated_pods] will only tick one checkbox although there is more than one associated pods.