Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django中扩展IntegrityError类_Python_Django_Exception - Fatal编程技术网

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
类的错误消息