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