On 8/8/06, Tom Tobin <[EMAIL PROTECTED]> wrote:
> On 5/6/06, Matthew Flanagan <[EMAIL PROTECTED]> wrote:
> >
> > On 5/6/06, Ivan Sagalaev <[EMAIL PROTECTED]> wrote:
> > >
> > > Matthew Flanagan wrote:
> > >
> > > >Since MR merged the _manipulator_validate_FIELD() methods stopped
> > > >working. The solution I came up with was to write custom manipulators
> > > >(which was what I was trying to avoid all along).
> > > >
> > > Can you assign those validators to the validator_list of a field right
> > > in a model? I never had a chance to use _manipulator_validate_FIELD() so
> > > I don't know if there is any difference.
> > >
> >
> > The validator functions assigned to a models validator_list only take
> > two arguments eg. isValidField(field_data, all_data). Where as they
> > _manipulator* ones looked like this _manipulator_validate_FIELD(self,
> > field_data, all_data), where 'self' was the manipulator instance. The
> > latter were automatically added to the model's manipulator in
> > pre-magic-removal.
>
> I know this is an old thread, but I just ran into the same issue on
> trunk: I have no idea how to duplicate the _manipulator_validate_FIELD
> behavior from pre-MR in order to access fields from the original
> object in a validator.  I can't find anything in the current docs or
> in the source which might indicate how to do this; anyone have any
> pointers?  :-)

Hooray!  I think I've figured it out:

1) Define a custom manager for the model.
2) Hang a validator (the name can be arbitrary, i.e., it doesn't have
to start with _validator_ or somesuch) off of the custom manager.
3) In the validator_list for the field in question, point to the
custom manager instance's attribute of the validator.
4) Inside the validator, the values of the existing object can be
accessed through self.values()

This is probably expressed more clearly through code:

class FooManager(models.Manager):
    def isSpamEggs(self, field_data, all_data):
        existing_parrot_value = self.values()['parrot']
        # do some validation stuff

class FooModel(models.Model):
    objects = FooManager()
    parrot = models.CharField(maxlength=50, validator_list=[objects.isSpamEggs])

Hopefully this will save someone else some head-desking; I'll see
about writing up a patch for the docs describing this (unless I
discover that I've been blind and this is, indeed, documented
somewhere).  :-D

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