multiple databases and multiple models in django

后端 未结 2 604
终归单人心
终归单人心 2020-11-27 13:57

I have two databases and two models:the Admin and the user.

I want to sync my models to the two databases; admin model to database A and user model to database B;

2条回答
  •  天涯浪人
    2020-11-27 14:46

    In order to define specific databases used for specific models, you need to define a database router:

    The easiest way to use multiple databases is to set up a database routing scheme. The default routing scheme ensures that objects remain ‘sticky’ to their original database (i.e., an object retrieved from the foo database will be saved on the same database). The default routing scheme ensures that if a database isn’t specified, all queries fall back to the default database.

    See this snippet as an example: http://djangosnippets.org/snippets/2687/

    Also see:

    • Django multi-database routing
    • Multiple Databases in Django
    • Tutorial: Using Django's Multiple Database Support
    • Database Routers in Django

提交回复
热议问题