Python 在Django中扩展IntegrityError类
我正在努力处理这个错误Python 在Django中扩展IntegrityError类,python,django,exception,Python,Django,Exception,我正在努力处理这个错误 django.db.utils.IntegrityError:重复的键值…. 问题是我不想捕获所有完整性错误,只想捕获其中包含消息“duplicate key value”的错误。例如,这就是我正在做的: try: activity.save() except IntegrityError as e: # if the error is not a key duplication one then throw again if 'duplicate
django.db.utils.IntegrityError:重复的键值….
问题是我不想捕获所有完整性错误,只想捕获其中包含消息“duplicate key value”的错误。例如,这就是我正在做的:
try:
activity.save()
except IntegrityError as e:
# if the error is not a key duplication one then throw again
if 'duplicate key value' not in e.args[0]:
raise e
我的问题是,是否有方法扩展IntegrityError
类,以便子类可以检查IntegrityError
类的错误消息