开发者

same form, different variables. how to implement?

I have a table where one of the fields is categories: Sports, Movies, Books, etc

I have another table with开发者_开发知识库 sub-categories, for instance sports is divided into ballet, futebol, golf, etc, and so on.

Let's say I've a list with two variables : ['Sport', 'Movies']

(These variables came from a file. I'm doing an upload file, and every time the category has more than one sub-option, I want to show a select with the option inside the category.)

for instance, the user inserted sport and movies. I want to show a select option for each one of the field inside the list. But the sub-options are not the same, it depends on the category chosen.

Output:

Sport:

o Futebol

o Ballet

o Golf

Movies:

o Drama

o Comedy

forms.py

class OptionsForm(forms.Form):
def __init__(self, numb, *args, **kwargs):
    super(OptionsForm, self).__init__(*args, **kwargs)
    self.fields['num'] = forms.ChoiceField(widget=RadioSelect(), choices=
        numb, label="", required=True)

I'm passing a tuple with all the sub-options:

numb = (('ballet', 'ballet'),
       ('futebol', 'futebol'),
       ('golf', 'golf'),  
       ('drama', 'drama'),
       ('comedy', 'comedy'))

but I'm not sure how to distinguish them, perhaps with formsets this will work, not sure how although

Any help or suggestion would be appreciate!

Thanks


Actually it was more easy than I was expected :)

I just needed to pass a dictionary of tuples:

dic = { 'Sport': [('ballet', 'ballet'), ('futebol', 'futebol'), ('golf'), ('golf')],
        'Movies': [('drama', 'drama'), ('comedy', 'comedy')] }

and change forms:

class CategoriesForm(forms.Form):
    def __init__(self, numb, *args, **kwargs):
        super(CtaegoriesForm, self).__init__(*args, **kwargs)
        for p, i in numb.items():
            self.fields[p] = forms.ChoiceField(widget=RadioSelect(), choices=i,
                required=True)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜