I got a way to do these, however a little magic because the logic in
django.db.models.manipulators.ManipulatorDescriptor.__get__ :
...
bases = [self.base]
if hasattr(model, 'Manipulator'): # here we have a
chance to add something to the auto-manipulator.
bases = [model.Manipulator] + bases
self.man = types.ClassType(self.name, tuple(bases), {})
self.man._prepare(model)
...
So I use the model.Manipulator field to add something to the default
automatic manipulator:
class CustomAutomaticManipulator(object):
def __init__(self, id=None):
if self.__class__.change: # call from model.ChangeManipulator
AutomaticChangeManipulator.__init__(self, obj_key=id,
follow=None)
else: # call from model.AddManipulator
AutomaticAddManipulator.__init__(self)
def add_validators(self, field_name, validator_list):
""" add extra validator_list to field"""
try:
field = [i for i in self.fields if i.field_name ==
field_name][0]
except IndexError:
raise NameError, 'no field named %' %field_name
field.validator_list.extend(validator_list)
class CategoryManipulator(CustomAutomaticManipulator):
def __init__(self, id=None):
CustomAutomaticManipulator.__init__(self, id)
self.add_validators('title_en', [self.validate_parent])
def validate_parent(self, field_data, all_data):
raise ValidationError('Your parent is error.')
class Category(models.Model):
Manipulator = CategoryManipulator # magic Manipulator
it worked but I don't think is a perfect way to do these, because
model.Manipulator is not documented. it belong to implemention rather
than a interface exported to public.
another way is to replace the default auto manipulator to ourselves.
but I don't find a good way to do these, I have to write a __meta__
field in our models class and call my parent's __meta__.__init__ and
replace the auto manipulator to oursevles.
any suggestion about the manipulator framework?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---