开发者

Django: Getting data from different database

I want to select data from a database table that is not part of my Django project. I added th开发者_开发知识库e 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.

Examples from the documentation:

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


sure, you just have to use

SomeObject.objects.using('database_name').all()

to do your queries

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜