Steven Armstrong wrote:
> On 08/10/06 20:25, DavidA wrote:
> > Steven Armstrong wrote:
> >> I've had similar problems and solved them like this:
> >>
> >> myproject/validators.py
> >> %<------------------------------
> >> from django.utils.translation import gettext_lazy as _
> >> from django.core.validators import *
> >> import re
> >>
> >> my_ansi_date_re = re.compile('put regexp here')
> >> def isValidANSIDate(field_data, all_data):
> >>      if not my_ansi_date_re.search(field_data):
> >>          raise ValidationError, _('Enter a valid date in PUT FORMAT
> >> DESCRIPTION HERE format.')
> >>
> >> [more custom or overridden validators]
> >> %<------------------------------
> >>
> >> And in my models I do:
> >> from myproject import validators
> >>
> >> instead of
> >>
> >> from django.core import validators
> >>
> >>
> >> Like this I can override any of djangos validators or add custom ones
> >> and they are all still available in one namespace.
> >>
> >> hope this helps
> >> cheers
> >> Steven
> >
> > Hi Steven,
> >
> > I tried your suggestion, but it still seems that Django is using the
> > isValidANSIDate funcion from django.core.validators rather than mine. I
> > put trace statements in django.core.validators.py as well as my
> > override and although I could see mine being imported after the core
> > ones, when the form's validation occurs, its using the core one. I
> > googled on "override module function in python" and got this hit:
> >
> > http://groups.google.com/group/comp.lang.python/browse_thread/thread/30bfdfd91d759275/6eaa3b20deb47d84%236eaa3b20deb47d84
> >
> > I've used the suggested technique in the following way and now it
> > works:
> >
> >     ---- my custom validators.py ----
> >     from pf.utils.convert import toDateRelaxed
> >
> >     def isValidANSIDate(field_data, all_data):
> >         try:
> >             toDateRelaxed(field_data)
> >         except ValueError:
> >             raise ValidationError, "Invalid date"
> >
> >     import django.core.validators
> >     django.core.validators.isValidANSIDate = isValidANSIDate
> >     ----
> >
> > And then I import my validators in my models.py class. The downside to
> > this, as explained in the link, is that if someone uses
> >
> >     from django.core.validators import isValidANSIDate
> >
> > they will get the original, not my custom version.
> >
> > Thanks for your suggestion as it got me on the right track.
> > -Dave
> >
>
> Hi Dave
>
> Good to hear you've found a solution.
>
> I have yet only used my approach with validators that I've set myself by
> passing them to the field constructors.
>
> Guess it fails with the date related fields because django imports and
> adds them automagically there.
>
> cheers
> Steven

Guess I spoke to soon. While my previous post successfully _validated_
dates of different formats, it didn't address _converting_ them to a
Python value. I ended up subclassing both models.DateField and
forms.DateField to get this to work. Seems like a lot of code to simply
allow different formats for entering dates, but at least it works.

-Dave


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to