I understand what you mean about the scope
Thanks for the sample code, very helpful, will try it out and let you
know.
Thanks,
On Feb 8, 2:45 pm, Daniel Roseman wrote:
> On Monday, February 7, 2011 8:27:55 PM UTC, SimpleDimple wrote:
>
> > I am new to Django and building a school system but am an experienced
> > developer otherwise having firm grip over rails, .net, php & java.
>
> > I have the following class where on teacher add/edit form, I am trying
> > to filter values in class drop down based on school. The value of
> > school_id is saved in session but as you can see below pulling value
> > from session fails in ModelForm, can someone please guide me on how to
> > get the value from session here ?
>
> > class TeacherForm(ModelForm):
> > def __init__(self, *args, **kwargs):
> > super(TeacherForm, self).__init__(*args, **kwargs)
> > xclass = self.fields['xclass'].widget
>
> > choices = []
>
> > #school_id = request.session['school_id'] # since this
> > fails, I have hard coded the value 2 in line below for now
> > school_id = 2
> > xclasses = Class.objects.filter(school=school_id)
> > for c in xclasses:
> > choices.append((c.id,c.name))
> > xclass.choices = choices
>
> It's fundamental to Python programming generally - and, I would have
> thought, Java and Ruby (although not PHP) - that if you want access to an
> object in a scope, you need to pass it into that scope. In order for you to
> access request.session within that __init__ method, you'll need to
> explicitly make the request object available there, which means passing it
> in when you initialise the form.
>
> I usually do it like this:
>
> class MyForm(forms.ModelForm):
> def __init__(self, *args, **kwargs):
> request = kwargs.pop('request')
> super(TeacherForm, self).__init__(*args, **kwargs)
> ...etc...
>
> now initialise the form in your view:
>
> form = MyForm(request=request)
>
> --
> DR.
--
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.