On Tue, Jul 31, 2012 at 1:23 AM, Sandeep kaur <mkaurkha...@gmail.com> wrote:
> I want to have a form, with drop down list and then multiple select
> checkboxes dynamically filtered based on drop down selection. I have
> searched a lot but did not find a complete solution for my
> requirement. Also I want to ask that can we do this without apply java
> query ?  Means if a browser doesn't support java, how would our could
> run?
>
> Help would be really appreciated.
 Lets say that you need a form with a drop-down list that have dynamic
values. With Django this can be done simple and fast.
MY_CHOICES = (
    ('1', 'Option 1'),
    ('2', 'Option 2'),
    ('3', 'Option 3'),
)

class MyForm(forms.Form):
    my_choice_field = forms.ChoiceField(choices=MY_CHOICES)

The form`s class has an __init__ method that is called on every form
load. Most of the times you skipped it in the form definition but now
you will have to use it.

def get_my_choices():
    # you place some logic here
    return choices_list

class MyForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['my_choice_field'] = forms.ChoiceField(
            choices=get_my_choices() )

You first call the __init__ method of the parent class(Form) using the
super keyword and then declare your dynamic fields(in this case
my_choice_field). With this code get_my_choices is called on every
form load and you will get your dynamic drop-down.

-- 
Satinderpal Singh
http://satindergoraya.blogspot.in/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to