Configuring multiple MongoDB databases in Django

Source: Internet
Author: User
Tags db2

Use the Mongoengine module when using the MongoDB database in Djnago. In Settings, configure the database connection as follows:

 from Import connectconnect ('dbname1', host='127.0.0.1', port= 27017)

The dbname1 is the name of the MongoDB database to be connected, and the host Ip,port the MongoDB database as the corresponding port.

The above is the configuration of a single database, if you want to configure multiple databases, one to increase the database connection in settings, and two to indicate the database used in the table defined in models.

The settings configuration is as follows:

 from Import connectconnect ('dbname1', host='127.0.0.1', port= 27017, alias='default') connect ('dbname2' , host='127.0.0.1', port=27017, alias='DB2' )

It adds a parameter alias, which is used to alias multiple databases to make it easier to specify the database to use in the models table, where there must be a default. When you do not specify a table to use the library, the default library defaults.

After the settings is set up, the models table is configured as follows:

 from Import *class  Test (Document):    = Stringfield ()    = Intfield ()        = {'db_alias'db2'}

Set the library to be used by the table, add element meta, specify the value of Db_alias (key), where the value is the database alias of the settings configuration.

Configuring multiple MongoDB databases in Django

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.