【问题标题】:Getting 'DatabaseError' object has no attribute 'message' in DJango获取 'DatabaseError' 对象在 DJango 中没有属性 'message'
【发布时间】:2018-05-23 10:34:24
【问题描述】:

我正在尝试在此处运行以下代码以将信息保存到数据库中。我看到了其他消息 - 但是 - 似乎解决方案适用于旧版本的 Python/DJango(因为它们似乎不适用于我现在使用的版本:Python 3.6.3DJango 1.11.7

if form.is_valid():
    try:
        item = form.save(commit=False)
        item.tenantid = tenantid
        item.save()
        message = 'saving data was successful'
    except DatabaseError as e:
        message = 'Database Error: ' + str(e.message)

这样做时,我会收到一条错误消息,下面列出的错误消息。如何解决此问题,以便打印出在 DB 级别找到的消息?

'DatabaseError' object has no attribute 'message'
Request Method:
POST
Request URL:
http://127.0.0.1:8000/storeowner/edit/
Django Version:
1.11.7
Exception Type:
AttributeError
Exception Value:
'DatabaseError' object has no attribute 'message'
Exception Location:
C:\WORK\AppPython\ContractorsClubSubModuleDEVELOP\libmstr\storeowner\views.py in edit_basic_info, line 40
Python Executable:
C:\WORK\Software\Python64bitv3.6\python.exe
Python Version:
3.6.3
Python Path:
['C:\\WORK\\AppPython\\ContractorsClubSubModuleDEVELOP',
 'C:\\WORK\\Software\\OracleInstantClient64Bit\\instantclient_12_2',
 'C:\\WORK\\Software\\Python64bitv3.6\\python36.zip',
 'C:\\WORK\\Software\\Python64bitv3.6\\DLLs',
 'C:\\WORK\\Software\\Python64bitv3.6\\lib',
 'C:\\WORK\\Software\\Python64bitv3.6',
 'C:\\Users\\dgmufasa\\AppData\\Roaming\\Python\\Python36\\site-packages',
 'C:\\WORK\\AppPython\\ContractorsClubSubModuleDEVELOP\\libintgr',
 'C:\\WORK\\AppPython\\ContractorsClubSubModuleDEVELOP\\libmstr',
 'C:\\WORK\\AppPython\\ContractorsClubSubModuleDEVELOP\\libtrans',
 'C:\\WORK\\AppPython\\ContractorsClubBackofficeCode\\libintgr',
 'C:\\WORK\\AppPython\\ContractorsClubBackofficeCode\\libmstr',
 'C:\\WORK\\TRASH\\tempforcustomer\\tempforcustomer\\libtempmstr',
 'C:\\WORK\\AppPython\\ContractorsClubBackofficeCode\\libtrans',
 'C:\\WORK\\Software\\Python64bitv3.6\\lib\\site-packages',
 'C:\\WORK\\Software\\Python64bitv3.6\\lib\\site-packages\\django-1.11.7-py3.6.egg',
 'C:\\WORK\\Software\\Python64bitv3.6\\lib\\site-packages\\pytz-2017.3-py3.6.egg']
Server time:
Sat, 9 Dec 2017 08:42:49 +0000

【问题讨论】:

  • 异常在 Python 3 中不再具有 message 属性。它们具有参数。请参阅 python.org/dev/peps/pep-0352/#transition-plan。

标签: django database


【解决方案1】:

一些变化

str(e.message)

HttpResponse(e.message)

【讨论】:

    【解决方案2】:

    改变一下

    str(e.message)
    

    str(e)
    

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 1970-01-01
      • 2014-05-12
      • 2019-06-09
      • 2023-02-01
      • 2021-02-03
      • 2022-12-09
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多