Please help with extra validation in admin

2 views
Skip to first unread message

Rob Slotboom

unread,
Oct 1, 2007, 3:09:51 PM10/1/07
to Django users
This is my model and I use a custom save to prevent the creation of a
category with a parent_id which is the same a the current_id (child of
itself)

class Category(models.Model):
name = models.CharField(maxlength=70, unique=True)
slug = models.SlugField(maxlength=50, unique=True,
prepopulate_from=('naam',))
parent = models.ForeignKey('self', blank=True, null=True,
limit_choices_to = {'parent__isnull': True})

def save(self):
try:
cur_id = int(self.id)
except:
cur_id = None
if cur_id and self.parent_id:
if int(self.parent_id) == cur_id:
raise validators.ValidationError("Same parent and child ")
super(Categorie, self).save()

I tried everything but I can't find a better approach than to use the
save method.
Custom validator: no object_id to check upon.
Custom manipulator: doesn't seem to work in admin.

So I cry for help on this one...

Rob Slotboom

unread,
Oct 3, 2007, 7:56:21 AM10/3/07
to Django users
Addition:

> This is my model and I use a custom save to prevent the creation of a
> category with a parent_id which is the same a the current_id (child of
> itself)

When an error occurs an ugly error message apears so I tried to create
a custom validator.
Custom validator didn't work because of "no object_id to check upon".
Creating a Custom manipulator doesn't seem to work in admin.

Can someone show me a solution to get the validation done in admin?

Thanks

Reply all
Reply to author
Forward
0 new messages