Django: Getting data from different database

后端 未结 2 1246
孤独总比滥情好
孤独总比滥情好 2021-02-07 18:03

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

相关标签:
2条回答
  • 2021-02-07 18:05

    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()
    
    0 讨论(0)
  • 2021-02-07 18:14

    sure, you just have to use

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

    to do your queries

    0 讨论(0)
提交回复
热议问题