[Chicago] Generate Dynamic lists

Sean Brant brant.sean at gmail.com
Fri Nov 4 15:17:10 CET 2011


On Thu, Oct 20, 2011 at 1:55 PM, Asif Jamadar <asif.jamadar at rezayat.net> wrote:
> So I'm trying to generate dynamic choices for  django form. Here i'm usig
> formset concept (CODE is mentioned below)
>
>
>
> Suppose i have list called criteria_list = ['education', 'know how',
> 'managerial', 'interpersonal', ]
>
>
>
> now i need to generate choices as follows
>
>
>
> list1 = [('education', 1), ('education', 2), ('education', 3), (''education'
> , 4) , ('know how', 1) ('know ho', 2), ('know ho', 3), ('know ho', 4)]
>
>
>
> list2 = [('education', 1), ('education', 2), ('education', 3), (''education'
> , 4) , ('managerial', 1) ('managerial', 2), ('managerial', 3),
> ('managerial', 4)]
>
>
>
> list3 = [('education', 1), ('education', 2), ('education', 3), (''education'
> , 4) , ('interpersonal', 1) ('interpersonal', 2), ('interpersonal', 3),
> ('interpersonal', 4)]
>
>
>
> list4 = [('know how', 1), ('know how', 2), ('know how ', 3), ('know how' ,
> 4) , ('managerial', 1) ('managerial', 2), ('managerial', 3), ('managerial',
> 4)]
>
>
>
> list5 = [('know how', 1), ('know how', 2), ('know how ', 3), ('know how' ,
> 4) , ('interpersonal', 1) ('interpersonal', 2), ('interpersonal', 3),
> ('interpersonal', 4)]
>
>
>
> list6= [('managerial', 1), ('managerial', 2), ('managerial ', 3),
> ('managerial' , 4) , ('interpersonal', 1) ('interpersonal', 2),
> ('interpersonal', 3), ('interpersonal', 4)]
>
>
>
>
>
> How can i achive this in python?
>
>
>
> The above all eachh list become the  choices for each form.
>
>
>
> Suppose i have formset of 6 forms. Then how can i assign above dynamic
> generates list to the choice field of each form.
>
>
>
> I tried by using this following code but no luck
>
>
>
>
>
> view.py
>
>
>
> def evaluation(request):
>
>
>
>     evaluation_formset = formset_factory(EvaluationForm,
> formset=BaseEvaluationFormset, extra=6)
>
>
>
>     if request.POST:
>
>
>
>         formset = evaluation_formset(request.POST)
>
>
>
>         ##validation and save
>
>
>
>     else:
>
>
>
>         formset = evaluation_formset()
>
>
>
>     render_to_response(formset)
>
>
>
> forms.py
>
>
>
>
>
> class EvaluationForm(forms.Form):
>
>
>
>     value =
> forms.ChoiceField(widget=forms.RadioSelect(renderer=HorizontalRadioRenderer))
>
>
>
> class BaseEvaluationFormSet(BaseFormSet):
>
>     def __init__(self, *args, **kwargs):
>
>         super(BaseEvaluationFormSet, self).__init__(*args, **kwargs)
>
>         for form_index, form in enumerate(self.forms):
>
>
>
>             form.fields["value"].choices = self.choice_method(form_index)
>
>     def choice_method(self, form_index):
>
>         list = []
>
>         item_list = []
>
>         criteria_list = []
>
>         criteria_length = len(sub_criterias)-1
>
>         for criteria_index in range(criteria_length):
>
>             counter = 1
>
>             if criteria_index == form_index:
>
>                 for j in range(criteria_length-counter):
>                     x = 1
>                     for i in range(6):
>                  criteria_list.append((sub_criterias[criteria_index],
> sub_criterias[criteria_index]))
>
>                         item_list.append((sub_criterias[criteria_index+ 1],
> sub_criterias[criteria_index+1]))
>
>                         list =  criteria_list +item_list
>
>                         counter = counter + 1
>                         if x != criteria_length:
>
>                             x = x + 1
>
>
>
>             return list
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>

The built-in zip function is a good place to start.
http://docs.python.org/library/functions.html#zip


More information about the Chicago mailing list