ModelForm/clean_
Hello List, I have a form inheriting from ModelForm. class C(ModelForm): class Meta: model = SomeModel def clean_somefield: enhanced checks Now my problem is how to access session data (request.session) from within the clean method. Does anyone have a clue how to this. gfk --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: ModelForm/clean_
On Mar 6, 10:37 am, gregor kling wrote: > Hello List, > > I have a form inheriting from ModelForm. > class C(ModelForm): > > > class Meta: > model = SomeModel > > def clean_somefield: > enhanced checks > > Now my problem is how to access session data (request.session) > from within the clean method. > Does anyone have a clue how to this. > > gfk Override the form's __init__ method to accept the request as a keyword parameter, and stash it in an instance attribute. class MyForm(forms.ModelForm): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super(MyForm, self).__init__(*args, **kwargs) def clean_somefield(self): .. do something with self.request... form = MyForm(request.POST, request=request, instance=myinstance) -- 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 -~--~~~~--~~--~--~---
Re: ModelForm/clean_
Daniel Roseman wrote: > On Mar 6, 10:37 am, gregor kling > wrote: >> Hello List, >> >> I have a form inheriting from ModelForm. >> class C(ModelForm): >> >> >> class Meta: >> model = SomeModel >> >> def clean_somefield: >> enhanced checks >> >> Now my problem is how to access session data (request.session) >> from within the clean method. >> Does anyone have a clue how to this. >> >> gfk > > Override the form's __init__ method to accept the request as a keyword > parameter, and stash it in an instance attribute. > > class MyForm(forms.ModelForm): > def __init__(self, *args, **kwargs): > self.request = kwargs.pop('request') > super(MyForm, self).__init__(*args, **kwargs) > > def clean_somefield(self): > .. do something with self.request... > > form = MyForm(request.POST, request=request, instance=myinstance) Thanks Daniel. That works like a charm. gfk > -- > 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 -~--~~~~--~~--~--~---
Get old value of Field in ModelForm clean_* method?
Is there any way to get the old value of a field in a clean_* method on the ModelForm? That is - the validation depends on the old value of the field versus the new value. self.cleaned_data only contains the new value. Thanks, Chris --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Get old value of Field in ModelForm clean_* method?
Your problem is a bit unclear, but i think what you are looking for is in self.data in your clean_ method On 12 Feb., 23:56, Chris Czub wrote: > Is there any way to get the old value of a field in a clean_* method > on the ModelForm? > > That is - the validation depends on the old value of the field versus > the new value. > > self.cleaned_data only contains the new value. > > Thanks, > > Chris --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Get old value of Field in ModelForm clean_* method?
No, that is the uncleaned data. Maybe a more explicit example will help: You go to edit an instance of a model that has a field named "title". The value of "title" for this particular instance is currently "Test". You change the value of "title" from "Test" to "Test123" and click "save". How can I get the original value of "title"(that is - "Test", not the new value, "Test123") in the clean_title method? On Thu, Feb 12, 2009 at 6:27 PM, Briel wrote: > > Your problem is a bit unclear, but i think what you are looking for is > in self.data in your clean_ method > > On 12 Feb., 23:56, Chris Czub wrote: >> Is there any way to get the old value of a field in a clean_* method >> on the ModelForm? >> >> That is - the validation depends on the old value of the field versus >> the new value. >> >> self.cleaned_data only contains the new value. >> >> Thanks, >> >> Chris > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Get old value of Field in ModelForm clean_* method?
On Thu, 2009-02-12 at 20:40 -0500, Chris Czub wrote: > No, that is the uncleaned data. > > Maybe a more explicit example will help: > > You go to edit an instance of a model that has a field named "title". > > The value of "title" for this particular instance is currently "Test". > > You change the value of "title" from "Test" to "Test123" and click "save". > > How can I get the original value of "title"(that is - "Test", not the > new value, "Test123") in the clean_title method? For a ModelForm, any passed in instance is stored in the "instance" attribute and the "object_data" attribute will be the data from the attributes. So you could access either of those (probably the latter, since it will be an empty dictionary if no instance is passed in, thus easy to separate out that case). Regards, Malcolm --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---