How to fix django's db_type deprecation warning?

白昼怎懂夜的黑 提交于 2019-12-11 20:14:24

问题


After upgrading to a more recent Django version, I started getting this deprecation warning:

Django version 1.3, using settings 'demos.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
/Users/.....myfile.py:328: DeprecationWarning: inner has been called without providing a connection argument. 
  if 'integer' in x.db_type()

I realized it's caused by the Field.db_type method, which returns the database column data type for a Field. This method has been modified so to comply with the multi-database feature of recent versions of Django, so now it also requires a connection object as argument [check the django docs]

But how to pass that connection object? I don't get it..


回答1:


... I found a solution that works. It's enough to import the connection from django.db, and pass that as an argument:

from django.db import connection 
if 'integer' in x.db_type(connection=connection):
    # do something...

Still wonder whether it's the right way of doing it though....



来源:https://stackoverflow.com/questions/8035078/how-to-fix-djangos-db-type-deprecation-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!