简体   繁体   English

引发与IntegrityError相关的异常时,获取字段名称的最pythonic方法是什么?

[英]What's the most pythonic way to get name of the field when an exception related to IntegrityError is thrown?

I'm developing an API with DRF and I want to generate and return proper custom error messages when an exception related to IntegrityError is thrown. 我正在使用DRF开发API,当引发与IntegrityError相关的异常时,我想生成并返回正确的自定义错误消息。

To do this, I've implemented a custom exception handler. 为此,我实现了一个自定义的异常处理程序。 Inside the custom exception handler, I want to get the name of the field that causes the error from the Exception instance and then I'll generate and return proper message in the response. 在自定义异常处理程序中,我想从Exception实例中获取导致错误的字段的名称,然后在响应中生成并返回正确的消息。

Currently, I can do this by parsing the message attribute of the Exception instance but I'm not sure this is the best possible solution. 当前,我可以通过解析Exception实例的message属性来做到这一点,但是我不确定这是否是最好的解决方案。

So, is there any pythonic way to get the name of the field from the Exception instance when an exception related to IntegrityError is thrown? 因此,当引发与IntegrityError相关的异常时,是否有任何pythonic方式可从Exception实例获取字段名称?

Thanks! 谢谢!

According to source it seems IntergityError is no more than an Exception so the only way is to parsing exception.message or exception.args . 根据消息来源 ,IntergityError似乎不只是一个Exception,所以唯一的方法是解析exception.messageexception.args

You always can check what gives you print(dir(exception)) but I'm pretty sure only message and args will be helpful. 您始终可以检查为您提供了print(dir(exception))但是我敢肯定,只有messageargs会有所帮助。

IMHO "parsing the message" is perfectly pythonic: simple, readable, you can do the same/similar solution in any other similar situation. 恕我直言,“解析消息”完全是Python风格的:简单,易读,您可以在任何其他类似情况下执行相同/相似的解决方案。 I've seen something like this before: 我以前看过这样的东西:

# in a custom Serializer's create() method
try:
    return super().create(validated_data)
except IntegrityError as e:
    raise APIException(detail=e.args[0].split('DETAIL:  ')[1])

It simple, it works and getting the extra data in any other way will likely involve something significantly more complicated. 它简单易行,并且以任何其他方式获取额外数据可能会涉及到非常复杂的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引发与意外文件类型相关的异常的最Pythonic方法是什么? - What is the most Pythonic way to raise an exception related to an unexpected file type? 确定字节序的最Pythonic方法是什么? - What's the most Pythonic way of determining endianness? 构建此词典的最pythonic方法是什么? - What's the most pythonic way to build this dictionary? 调用函数时以Python方式获取“源代码”的方式是什么? - What's the Pythonic way to get the 'source' when a function is called? 重构此列表构建代码的最Pythonic方法是什么? - What's the most Pythonic way to refactor this list building code? 什么是访问C库的最pythonic方式 - 例如,OpenSSL? - What's the most pythonic way of access C libraries - for example, OpenSSL? 从字符串的开头删除数字的最Pythonic方法是什么? - What's the most Pythonic way to remove a number from start of a string? 重构这些嵌套的for / else循环的最pythonic方法是什么(如果有的话)? - What's the most pythonic way (if any) to refactor these nested for/else loops? 编写此筛选列表理解的最pythonic方法是什么? - What's the most pythonic way to write this filtered list comprehension? 识别列表中连续重复项的最 Pythonic 方法是什么? - What's the most Pythonic way to identify consecutive duplicates in a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM