Custom validation for models in admin
Hi all, I have a model which needs some non-standard checks before it gets saved. The class looks like this : class Page(models.Model): name = models.CharField(maxlength=30) content = models.TextField() start_date = models.DateField() end_date = models.DateField() When I save the model, I want to check that there is no other page with the same name that has an overlapping start and end date, and cause some kind of validation error that the admin area will display. I have tried the following : def save(self): # check for the overlap happens here # if the check fails... raise validators.ValidationError(_("A page with this name already exists in the time period specified")) However, the admin area does not catch the exception. I've looked through the djangobook which references a chapter that isn't there yet, and the online documentation points to a .validate() on the model which is marked as experimental. If it's possible, where can I put custom validation like that, that the admin area will pick up on? I'm currently using 0.96 but could easily upgrade to the svn trunk if needed. Thanks for any help, Mike --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Custom validation for models in admin
You need to write a validator and use validator_list in the field attributes to do custom validation in admin. http://www.djangoproject.com/documentation/model-api/#validator-list --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Custom validation for models in admin
Sorry, I should have said that I'd already tried a custom validator. Unfortunately, validators don't provide the functionality I need. I need to check in the database to see if there are any pages, other than the one I am saving, that have start and end dates that overlap with the one I am saving. To do that, I need to know the id of the object I am saving, or if I am trying to save a new object. Validators do not tell me that as the id is not passed in inside all_data. Is there anywhere else I can add this validation? Or is there a type of exception I can raise that the admin area will display as an error and let the user correct the data? Cheers, Mike Collin Grady wrote: > You need to write a validator and use validator_list in the field > attributes to do custom validation in admin. > > http://www.djangoproject.com/documentation/model-api/#validator-list > > > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Custom validation for models in admin
Hi Mike, I have the same problem with my model. (Link below) Reading your "points to a .validate() on the model" raises the following question. Have you tried it? http://groups.google.com/group/django-users/browse_thread/thread/bd2b2c24f3879690 --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Custom validation for models in admin
The following did it for me: from django import newforms as forms class yourForm(forms.BaseForm): ... def clean(self): "do your custom validation here" return self.cleaned_data does it for me. Clean is called automatically e.g. by ...is_valid(). hope it helps. jr On Sep 11, 10:19 pm, Mike H <[EMAIL PROTECTED]> wrote: > Hi all, > > I have a model which needs some non-standard checks before it gets saved. > The class looks like this : > > class Page(models.Model): > name = models.CharField(maxlength=30) > content = models.TextField() > start_date = models.DateField() > end_date = models.DateField() > > When I save the model, I want to check that there is no other page with > the same name that has an overlapping start and end date, and cause some > kind of validation error that the admin area will display. > > I have tried the following : > > def save(self): > # check for the overlap happens here > > # if the check fails... > raise validators.ValidationError(_("A page with this name > already exists in the time period specified")) > > However, the admin area does not catch the exception. I've looked > through the djangobook which references a chapter that isn't there yet, > and the online documentation points to a .validate() on the model which > is marked as experimental. If it's possible, where can I put custom > validation like that, that the admin area will pick up on? I'm currently > using 0.96 but could easily upgrade to the svn trunk if needed. > > Thanks for any help, > > Mike --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---