Django - Checkboxes for Models, Two Submit Buttons_ Add Person Model to Group Model, Reject Person...
Transcript of Django - Checkboxes for Models, Two Submit Buttons_ Add Person Model to Group Model, Reject Person...
-
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
Clash1,036 14 28
I'velookedatformsetandmodelformsetatDjangomanytimes,butIstillcan'tfigurethesmartwaytodothis.
Ihavetwomodels:
Group
Person
Ihaveaquerysetthatcontainsallthepersonstryingtojoinaparticulargroup:Person.objects.filter(wantsToJoinGroup=groupD)
Now,whatIwanttodo,isdisplayapagewithacheckboxatthesideofeachpersonthatwantstojoinaparticulargroup.Thosecheckboxescanthenbecheckedandthenthebutton'AccepttoGroup'isclicked.Iwantthistobulkaddthosepersonstoacertaingroup.
WhatIfailtounderstandhowtodoisexactlythecheckboxthing.I'vebeentryingtoextendamodelformandthenmakea formsetoutofit,butIfailatiteverytime.ItseemslikeifIwanttodoaformsetwithmodelsIshoulduse modelformset,butthatdoesnotallowmetoextendtheformtoaddacheckbox.HowcanIdoit?
Hereisa10seconddraftonpaintofwhatIwouldliketohave:
Soit'sbasically,acheckboxandawaytoaccessthepersonmodelatthetemplateandthenawaytoproccessthisontheview.
Thanksinadvance!
Edit:Bytheway,beforesomeonesuggestsusing ModelMultipleChoiceField,unlessthereisawaytoaccesseachoftheobjectsinsideitonthetemplate,thiswillnotfulfillwhatIneedtodo.AsfarasIknow,Ican'titerateovertheobjectsofModelMultipleChoiceFieldonthetemplate.PleasecorrectmeifI'mwrong!
django djangomodels djangoforms
editedJan25'10at18:37 askedJan25'10at17:51
StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free,noregistrationrequired.
Checkboxes for models, two submit buttons: add person model to group model, rejectperson 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
DrBloodmoney1,848 2 9 15
DmitryShevchenko10.9k 4 26 38
dannyroa1,951 1 11 35
Ifyou'renotmarriedtotheideaofusingamodelform,Iwouldjustusearegularform,withaModelMultipleChoiceField,giveitaquerysetinthe __init__thenprovidethatsamequerysettothetemplatecontext(toiterateoveratyourleisure):
#viewdefadd_to_group(request):persons=Person.objects.filter(wantsToJoinGroup=groupD)ifrequest.POST:form=PersonAddForm(persons,request.POST)ifform.is_valid():#yourhandlinglogicform=PersonAddForm(persons)context={'persons':persons,'form':form}returnrender_to_response(template,context)
#formclassPersonAddForm(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,neededtousekwargs.pop('queryset',None)togetittowork,thanks! Clash Jan25'10at22:48
Youshouldn'thavetopopit,becauseyouarepassingitasapositionalargumentandnotakeyword.Iusethispatternagoodbit.You'llhavetopasstherequest.POSTtotheformtovalidate(onPOSTobviously),butnotonget.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'.OntheonclickeventoftheAcceptandRejectbuttons,setthevalueofthehiddenfieldappropriatelyandthensubmittheform.
Inyourview,checkthevalueofthehiddenfieldtofigureoutifit'sAcceptorReject.
answeredJan25'10at18:16
HelloDanny,thankyouforanswer...IwaslookingforsomethingontheDjangoside,nojavascriptinvolvedpreferentiallyRegardingthecheckboxes,howcantheybedone? Clash Jan25'10at18:18
Notsureifthatispossiblewithoutjavascriptbecauseyoucannotselectivelysubmitformfields.Ifyoudon'twantjavascript,makeAcceptandRejectradiobuttonsandhaveaseparatesubmitbutton.dannyroaJan25'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.