On Mon, Aug 31, 2009 at 6:27 PM, aa56280 <aa56...@gmail.com> wrote: > > I can't find an answer to this, so I'm hoping folks here can help: why > is it that Django catches IntegrityError for a field with unique=True > in the Admin tool but requires you to catch it yourself in your app? > That is, why isn't it handled like all other built-in validation > checks that come bundled with other options like, say, max_length? >
It is handled like other built-in validation checks. Within admin both max_length, unique, etc. are checked/validated through use of a ModelForm for the model. If your code uses ModelForms you will get the same validation checks. If you don't use forms you won't (as presently there is no model validation). Depending on the database you can get an exception raised as easily for violating max_length as you can for violating uniqueness. For example, given this model: class Foo(models.Model): name = models.CharField(max_length=2, unique=True) and using a MySQL db with DEBUG on, attempting to create a model that violates the max_length constraint will raise an exception: >>> from ttt.models import Foo >>> Foo.objects.all() [] >>> Foo.objects.create(name='Yeppers') Traceback (most recent call last): File "<console>", line 1, in <module> [snip] File "/usr/lib/python2.5/warnings.py", line 102, in warn_explicit raise message Warning: Data truncated for column 'name' at row 1 >>> MySQL considers that just a warning (though there may well be a configuration option to tell it whether to treat this is a warning or error situation, I haven't checked) so you need to have DEBUG True for that to raise an exception, and MySQL has saved the model anyway with the truncated data. Sqlite won't enforce the max_length check at all, I don't believe. Not sure off the top of my head how others behave. If you then try to add another object that violates the unique constraint again you get an exception if you do it directly by attempting to create the model: >>> Foo.objects.create(name='Ye') Traceback (most recent call last): File "<console>", line 1, in <module> [snip] File "/var/lib/python-support/python2.5/MySQLdb/connections.py", line 35, in defaulterrorhandler raise errorclass, errorvalue IntegrityError: (1062, "Duplicate entry 'Ye' for key 2") >>> However if you use a ModelForm to validate the data prior to attempting to save the model you get these problems reported as validation errors: >>> from django import forms >>> class FooForm(forms.ModelForm): ... class Meta: ... model = Foo ... >>> ff = FooForm(data={'name': 'Yeppers'}) >>> ff.is_valid() False >>> ff.errors {'name': [u'Ensure this value has at most 2 characters (it has 7).']} >>> ff = FooForm(data={'name': 'Ye'}) >>> ff.is_valid() False >>> ff.errors {'name': [u'Foo with this Name already exists.']} >>> Since admin uses ModelForms, you see form validation errors when using it instead of exceptions. Karen --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---