[Answered ]-Checkboxes for models, two submit buttons: add person model to group model, reject person model from group model

1👍

If you’re not married to the idea of using a modelform, I would just use a regular form, with a ModelMultipleChoiceField, give it a queryset in the __init__ then provide that same queryset to the template context (to iterate over at your leisure):

#view
def add_to_group(request):
    persons = Person.objects.filter(wantsToJoinGroup=groupD)
    if request.POST:
        form = PersonAddForm(persons, request.POST)
        if form.is_valid():
            #your handling logic
    form = PersonAddForm(persons)
    context = {'persons': persons, 'form': form}
    return render_to_response(template, context)

#form
class PersonAddForm(forms.Form):
    def __init__(self, queryset, *args, **kwargs):
        super(PersonAddForm, self).__init__(*args, **kwargs)
        self.fields['persons'] = forms.ModelMultipleChoiceField(queryset=queryset,
                                     widget=forms.CheckboxSelectMultiple() )

1👍

You can actually get to ModelMultipleChoiceField’s items this way:

my_field.field.queryset

where my_field is an instance of ModelMultipleChoiceField.

0👍

At the top of my head, you can insert a hidden field called ‘Action’. On the onclick event of the Accept and Reject buttons, set the value of the hidden field appropriately and then submit the form.

In your view, check the value of the hidden field to figure out if it’s Accept or Reject.

Leave a comment