Django:从不同的数据库获取数据 [英] Django: Getting data from different database

查看:60
本文介绍了Django:从不同的数据库获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从不属于Django项目的数据库表中选择数据.我将数据库连接信息添加到设置文件中,并且可以对它执行原始sql查询以提取数据.但是,我想为该表创建一个模型,并能够像访问其他Django模型数据一样访问数据.

I want to select data from a database table that is not part of my Django project. I added the database connection info to the settings file and I can do raw sql queries against it to pull the data. However, I would like to create a model for that table and be able to access the data as I would any other Django model data.

这可能吗?我可以找到任何有关的文档.

Is this possible? I can find any documentation on that.

推荐答案

The Django: Multiple Databases page contains good information on this topic. After you have configured your databases in settings.py, you can use .using() to specify the database you wish to query.

文档中的示例:

>>> # This will run on the 'default' database.
>>> Author.objects.all()

>>> # So will this.
>>> Author.objects.using('default').all()

>>> # This will run on the 'other' database.
>>> Author.objects.using('other').all()

这篇关于Django:从不同的数据库获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆