如何修复 django 的 db_type 弃用警告?

发布于 2024-12-14 07:47:11 字数 612 浏览 3 评论 0原文

升级到更新的 Django 版本后,我开始收到此弃用警告:

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()

我意识到这是由 Field.db_type 方法引起的,该方法返回 Field 的数据库列数据类型。此方法已被修改,以便符合 Django 最新版本的多数据库功能,因此现在它还需要一个连接对象作为参数 [检查 django 文档]

但是如何传递该连接对象?我不明白..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小巷里的女流氓 2024-12-21 07:47:11

...我找到了一个有效的解决方案。从 django.db 导入连接并将其作为参数传递就足够了:

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

仍然想知道这是否是正确的方法......

... 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....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文