On 24 fév, 21:11, harryos <oswald.ha...@gmail.com> wrote: > sorry, > missed while copying > > def save(self): > self.name=self.name.strip() > if (not self.pk): > if Article.objects.filter(name__iexact=self.name).count()! > =0: > raise Exception('article exists..')
Now this will only check for duplicate names if it's a new article !-) The correct test would be: others = Article.objects.filter(name__iexact=self.name) if self.pk: others = other.exclude(pk=self.pk) # zero has a false value in boolean expressions # so you don't need an explicit test: if others.count(): # use a more specific Exception - possibly # one you defined yourself raise UniqueConstraintViolation("yadda yadda") # ok, proceed... super(Article, self).save(*args, **kw) But anyway: you already specified the unique constraint in the 'name' field definition, so adding this code in your model save() method is just useless - the uniqueness constraint WILL be applied at the db level. > this causes 500 error,and causes the traceback displayed on > browser..how can I prevent this crash and give the user some useful > warning? Do the test in your ModelForm 'clean_name' method and raise a ValidationError there. Useful documentation here: http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms HTH -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.