[Python-Dev] Generate Dynamic lists
Jacek Pliszka
jacek.pliszka at gmail.com
Thu Oct 20 21:13:37 CEST 2011
I believe this is not the correct forum for this as it does not
concern development of Python language itself - you should post to
comp.lang.python. However solutions is relatively simply, try this:
list1,list2,list3,list4,list5,list6 = [ list(itertools.product(i,range(0,4)))
for i in itertools.combinations(criteria_list,2) ]
Best Regards,
Jacek Pliszka
2011/10/20 Asif Jamadar <asif.jamadar at rezayat.net>:
> 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
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/jacek.pliszka%40gmail.com
>
>
More information about the Python-Dev
mailing list