开发者

Checkboxes for models, two submit buttons: add person model to group model, reject person model from group model

I've looked at formset and model formset at Django many times, but I still can't figure the smart way to do this.

I have two models:

Group

Person

I have a queryset that contains all the persons trying to join a particular group: Person.objects.filter(wantsToJoinGroup=groupD)

Now开发者_开发知识库, what I want to do, is display a page with a checkbox at the side of each person that wants to join a particular group. Those checkboxes can then be checked and then the button 'Accept to Group' is clicked. I want this to bulk add those persons to a certain group.

What I fail to understand how to do is exactly the checkbox thing. I've been trying to extend a modelform and then make a formset out of it, but I fail at it everytime. It seems like if I want to do a formset with models I should use modelformset, but that does not allow me to extend the form to add a checkbox. How can I do it?

Here is a 10-second draft on paint of what I would like to have:

alt text http://img403.imageshack.us/img403/1002/draft.png

So it's basically, a checkbox and a way to access the person model at the template and then a way to proccess this on the view.

Thanks in advance!

Edit: By the way, before someone suggests using ModelMultipleChoiceField, unless there is a way to access each of the objects inside it on the template, this will not fulfill what I need to do. As far as I know, I can't iterate over the objects of ModelMultipleChoiceField on the template. Please correct me if I'm wrong!


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() )


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

my_field.field.queryset

where my_field is an instance of ModelMultipleChoiceField.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜