Está en la página 1de 3

12/6/2014 django - Checkboxes for models, two submit buttons: add person model to group model, reject person

model from gro


http://stackoverflow.com/questions/2134341/checkboxes-for-models-two-submit-buttons-add-person-model-to-group-model-reje 1/3
Takethe2minutetour
Clash
1,036 14 28
I'velookedatformsetandmodelformsetatDjangomanytimes,butIstillcan'tfigurethesmartwayto
dothis.
Ihavetwomodels:
Group
Person
Ihaveaquerysetthatcontainsallthepersonstryingtojoinaparticulargroup:
Person.objects.filter(wantsToJoinGroup=groupD)
Now,whatIwanttodo,isdisplayapagewithacheckboxatthesideofeachpersonthatwantstojoina
particulargroup.Thosecheckboxescanthenbecheckedandthenthebutton'AccepttoGroup'is
clicked.Iwantthistobulkaddthosepersonstoacertaingroup.
WhatIfailtounderstandhowtodoisexactlythecheckboxthing.I'vebeentryingtoextenda
modelform andthenmakea formset outofit,butIfailatiteverytime.ItseemslikeifIwanttodoa
formset withmodelsIshoulduse modelformset ,butthatdoesnotallowmetoextendtheformto
addacheckbox.HowcanIdoit?
Hereisa10seconddraftonpaintofwhatIwouldliketohave:
Soit'sbasically,acheckboxandawaytoaccessthepersonmodelatthetemplateandthenawayto
proccessthisontheview.
Thanksinadvance!
Edit:Bytheway,beforesomeonesuggestsusing ModelMultipleChoiceField ,unlessthereisa
waytoaccesseachoftheobjectsinsideitonthetemplate,thiswillnotfulfillwhatIneedtodo.Asfaras
Iknow,Ican'titerateovertheobjectsofModelMultipleChoiceFieldonthetemplate.Pleasecorrectmeif
I'mwrong!
django djangomodels djangoforms
editedJan25'10at18:37 askedJan25'10at17:51
StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free,no
registrationrequired.
Checkboxes for models, two submit buttons: add person model to group model, reject
person model from group model
signup

login

tour

help

careers2.0

12/6/2014 django - Checkboxes for models, two submit buttons: add person model to group model, reject person model from gro
http://stackoverflow.com/questions/2134341/checkboxes-for-models-two-submit-buttons-add-person-model-to-group-model-reje 2/3
3 Answers
DrBloodmoney
1,848 2 9 15
DmitryShevchenko
10.9k 4 26 38
dannyroa
1,951 1 11 35
Ifyou'renotmarriedtotheideaofusingamodelform,Iwouldjustusearegularform,witha
ModelMultipleChoiceField,giveitaquerysetinthe __init__ thenprovidethatsamequerysettothe
templatecontext(toiterateoveratyourleisure):
#view
defadd_to_group(request):
persons=Person.objects.filter(wantsToJoinGroup=groupD)
ifrequest.POST:
form=PersonAddForm(persons,request.POST)
ifform.is_valid():
#yourhandlinglogic
form=PersonAddForm(persons)
context={'persons':persons,'form':form}
returnrender_to_response(template,context)
#form
classPersonAddForm(forms.Form):
def__init__(self,queryset,*args,**kwargs):
super(PersonAddForm,self).__init__(*args,**kwargs)
self.fields['persons']=forms.ModelMultipleChoiceField(queryset=queryset,
widget=forms.CheckboxSelectMultiple())
editedJan25'10at22:54 answeredJan25'10at19:50
Hello!Thankyouforyouranswer!I'mtryingyoursolution,butmyformisalwaysreturingfalseforis_valid(),
anyideawhy? Clash Jan25'10at22:28
JustfiguredIneededtopassrequest.POSTandquerysetargtotheform...bytheway,neededtouse
kwargs.pop('queryset',None) togetittowork,thanks! Clash Jan25'10at22:48
Youshouldn'thavetopopit,becauseyouarepassingitasapositionalargumentandnotakeyword.Iuse
thispatternagoodbit.You'llhavetopasstherequest.POSTtotheformtovalidate(onPOSTobviously),but
notonget.I'lledittobeexplicit.DrBloodmoneyJan25'10at22:54
Ah,Isee,soIsenditasanonkeywordedarg!Thankyou Clash Jan25'10at23:03
YoucanactuallygettoModelMultipleChoiceField'sitemsthisway:
my_field.field.queryset
wheremy_fieldisaninstanceofModelMultipleChoiceField.
answeredJan25'10at20:09
Thankyouforyouranswer! Clash Jan25'10at22:29
Atthetopofmyhead,youcaninsertahiddenfieldcalled'Action'.OntheonclickeventoftheAccept
andRejectbuttons,setthevalueofthehiddenfieldappropriatelyandthensubmittheform.
Inyourview,checkthevalueofthehiddenfieldtofigureoutifit'sAcceptorReject.
answeredJan25'10at18:16
HelloDanny,thankyouforanswer...IwaslookingforsomethingontheDjangoside,nojavascriptinvolved
preferentiallyRegardingthecheckboxes,howcantheybedone? Clash Jan25'10at18:18
Notsureifthatispossiblewithoutjavascriptbecauseyoucannotselectivelysubmitformfields.Ifyoudon't
wantjavascript,makeAcceptandRejectradiobuttonsandhaveaseparatesubmitbutton.dannyroaJan
25'10at20:28
addcomment
addcomment
addcomment
addcomment
12/6/2014 django - Checkboxes for models, two submit buttons: add person model to group model, reject person model from gro
http://stackoverflow.com/questions/2134341/checkboxes-for-models-two-submit-buttons-add-person-model-to-group-model-reje 3/3
Not the answer you're looking for? Browse other questions tagged django
django-models django-forms or ask your own question.

También podría gustarte