>From my code,
         try:
            user = User.objects.get(username__iexact =
self.cleaned_data['username'])
        except User.DoesNotExist, KeyError:
            raise forms.ValidationError('Invalid username, please try
again.')

        if not user.check_password(self.cleaned_data['password']):
            raise forms.ValidationError('Invalid password, please try
again.')

So I just want to check that user exists and knows correct password.
The reason I do this in clean is as the order of field.clean is not
guaranteed, I would have to do User.objects.get(username__iexact =
self.cleaned_data['username']) two times with two database hits.
(Probably premature optimisation :) )So essentially what I want to
know should I be always checking
 ``if field in self.cleaned_data:``
before using them in form.clean?

On Apr 9, 7:02 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Mon, 2008-04-07 at 07:29 -0700, shabda wrote:
> > So is there a cleaner way to work in form.clean instead of doing
> > if for.cleaned_datahas_key(field):
> > for every element?
>
> It depends what you want to do in the clean() method. The example you
> gave in your first post in this thread is not a good use of clean(),
> since you are just doing single field validation, so you might as well
> have written clean_username and clean_password methods, except you
> didn't need those either because you were just testing if the fields
> were present and for required fields, the default cleaning methods will
> flag that as an error in any case. Basically, the initial example you
> posted needed no extra cleaning methods beyond what Django provides by
> default.
>
> If you're trying to do something else in clean(), explain what you're
> wanting to achieve and we might be able to suggest something.
>
> Regards,
> Malcolm
>
> --
> Depression is merely anger without 
> enthusiasm.http://www.pointy-stick.com/blog/
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to