Today, the web framework Django How to configure the use of the database, that is, the legendary MVC (model View Controller) in the M,model (models).
Briefly describe the MVC in Django:
Model: Define your database, typically in a models.py file.
View: Define your HTML and other static Web file related, that is, HTML, CSS, JS and other front-end things.
Controller: Define your business logic related, which is your main code.
Body Start
First, there are two files found in your Django project:setting.py, models.py
Then make sure your app is registered to the setting inside yo
Locate the databases, where the configuration database is located, and then set:
First, if the project is created, then your databases should look like this:
The Django source defaults to the self-sqlite3 databaseused. If you want to use your own, then you can simply not move the settings here, if you want to use a different database, change the ENGINE value, such as Mysql:django.db.backends.mysql
Other parameters must be set when we use other databases, or Django may not even be able to connect to the database you specify , I'll take MySQL for example.
You can see that there are some parameters, name is the name of the database you want to use (the implementation to create good, or Django can not find), the user is the database login account, followed by the password, IP, port.
Here setting should be the configuration is complete, and then open models.py, for editing
Finally, through CMD, go to your Django directory and execute the python manage.py makemigrations
Then execute the python manage.py migrate
Success ok! Sprinkle flowers
If you want to do multi-database configuration, the study of some, and finally straightened out, it is indeed more complex. But it is not very complicated, more than two or three steps, below we one step to
The first thing you need to do is to continue adding a dictionary under Database, for example, I've added a yq that writes out the database I'm yq to reference.
Then add router database_routers and map database_apps_mapping
See the database_routers = [' Yq_djago.database_router. Databaseappsrouter '] , this is the address of the router, which means to use the Databaseappsrouter method inside the Database_router file in the Yq_djago project
So now we're going to create a database_router.py file under this path, and then edit it as follows
fromDjango.confImportsettingsdatabase_mapping=settings. Database_apps_mappingclassDatabaseappsrouter (object):"""A router to control all database operations on models for different databases. In the case a app is not a set in settings. Database_apps_mapping, the router would fallback to the ' default ' database. Settings example:database_apps_mapping = {' App1 ': ' db1 ', ' app2 ': ' DB2 '}""" defDb_for_read (self, model, * *hints):""""Point all read operations to the specific database.""" ifModel._meta.app_labelinchdatabase_mapping:returnDatabase_mapping[model._meta.app_label]returnNonedefDb_for_write (self, model, * *hints):"""Point all write operations to the specific database.""" ifModel._meta.app_labelinchdatabase_mapping:returnDatabase_mapping[model._meta.app_label]returnNonedefAllow_relation (self, obj1, obj2, * *hints):"""Allow any relation between apps this use the same database."""db_obj1=database_mapping.get (Obj1._meta.app_label) db_obj2=database_mapping.get (Obj2._meta.app_label)ifDb_obj1 andDb_obj2:ifDb_obj1 = =Db_obj2:returnTrueElse: returnFalsereturnNonedefallow_syncdb (self, DB, model):"""Make sure this apps only appear in the related database.""" ifDbinchdatabase_mapping.values ():returnDatabase_mapping.get (model._meta.app_label) = =DBelifModel._meta.app_labelinchdatabase_mapping:returnFalsereturnNonedefAllow_migrate (self, DB, App_label, Model=none, * *hints):"""Make sure the Auth app is appears in the ' auth_db ' database. """ ifDbinchdatabase_mapping.values ():returnDatabase_mapping.get (app_label) = =DBelifApp_labelinchdatabase_mapping:returnFalsereturnNone
Finally, in the models.py to specify which data our table uses, and continue to take examples of my own example
Finally, through CMD, go to your Django directory and execute the python manage.py makemigrations
Then create the database Yq, execute the python manage.py migrate--database=yq (not write--database is the default creation of the inside)
Open database view, done! Created interface and Interface2 two tables are in the inside, sprinkle flowers!!!
Django Models database configuration and multiple database settings