At the current code, we could only hopes that all database backend modules fully support Python DB API 2.0, in this case the Exception tree. I did a simple dir() and looks like MySQLdb, psyopg1, and sqlite3 all support the exceptions, therefore you can reliably catch IntegrityError exception by wrapping the save() call like this:

   from django.core.db import dbmod
   try:
       u2.save()
   except dbmod.Database.IntegrityError:
       //handle error
       pass

Though, a better way in your case is improving the view code to check existing user with the same username ahead of time, and rely less on catching the database exception since not every database trigger the same exception class on the same error.

Amit Upadhyay wrote:
Hi,

What is the rigth way of catching IntegrityError, the traceback I get suggests using _mysql_exceptions.IntegrityError, which is wrong because it assumes mysql, as well as it goes against the general python guideline of not using "hidden" members of modules.

Reply via email to