Django uses the MySQL database process

Source: Internet
Author: User
Tags db2

1. Data migration
Different apps connect different databases (examples of blogs with default database, BLOG4 with MySQL database)

(1) to connect MySQL database, first download MySQL database and MySQLdb module

Download the MySQL Database reference connection:

Http://www.cnblogs.com/onlycxue/p/3291889.html
and MYSQLDB Module Installation reference connection: https://pypi.python.org/pypi/mysql-

python/1.2.4 (I am the download. exe)


(2) The next step is to create a new Django project or something that is not detailed (my project has blog and blog4 two apps


(3) Modify the setting.py configuration file
DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.sqlite3 ',
' NAME ': Os.path.join (Base_dir, ' db.sqlite3 '),
},
' DB2 ': {
' ENGINE ': ' Django.db.backends.mysql ',
' NAME ': ' Dbname2 ',
' USER ': ' Root ',
' PASSWORD ': ' 5302613 ',
' HOST ': ' 127.0.0.1 ',
},
}

Database_routers = [' Mysite.database_router. Databaseappsrouter ']
Database_apps_mapping = {
' Blog4 ': ' DB2 ',
}

(4) The Code of the Blog4 models.py file is as follows:

From __future__ import unicode_literals
From django.db import Models

# Create your models here.
Class Blog4 (models. Model):
title = models. Charfield (max_length=100)
Content = models. TextField ()

def __unicode__ (self):
Return Self.title

(5) Add a database_router.py file in the project folder mysite.py folder with the same name
Code on this site copy: http://www.ziqiangxuetang.com/django/django-multi-

Database.html

(7) Enter the project directory, enter the command:
>>>python manage.py makemigrations

Migrations for ' blog4 ':
blog4\migrations\0001_initial.py:
-Create Model BLOG4


>>>mysql-u root-p; (go to MySQL database)

>>>create database dbname2;

mysql> CREATE DATABASE dbname2;
Query OK, 1 row Affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| Information_schema |
| dbname1 |
| dbname2 |
| MySQL |
| Performance_schema |
| Test |
+--------------------+
6 rows in Set (0.00 sec)

I can see that the Dbname2 database I built was successful.


>>>python manage.py Migrate--database=db2 (sync to MySQL database for blog3)

Operations to perform:
Apply all migrations:admin, auth, blog, blog2, Blog3, Blog4, ContentTypes, SE
Ssions
Running Migrations:
Rendering model states ... Done
Applying contenttypes.0001_initial ... Ok
Applying auth.0001_initial ... Ok
Applying admin.0001_initial ... Ok
Applying Admin.0002_logentry_remove_auto_add ... Ok
Applying Contenttypes.0002_remove_content_type_name ... Ok
Applying auth.0002_alter_permission_name_max_length ... Ok
Applying auth.0003_alter_user_email_max_length ... Ok
Applying auth.0004_alter_user_username_opts ... Ok
Applying Auth.0005_alter_user_last_login_null ... Ok
Applying auth.0006_require_contenttypes_0002 ... Ok
Applying auth.0007_alter_validators_add_error_messages ... Ok
Applying auth.0008_alter_user_username_max_length ... Ok
Applying blog.0001_initial ... Ok
Applying blog2.0001_initial ... Ok
Applying blog3.0001_initial ... Ok
Applying blog4.0001_initial ... Ok
Applying blog4.0002_auto_20160323_1429 ... Ok
Applying sessions.0001_initial ... Ok

See above, prove sync to MySQL database succeeded!!!


Go to MySQL database to see if it is successful:

mysql> use dbname2;
Database changed
Mysql> Show tables;
+----------------------------+
| tables_in_dbname2 |
+----------------------------+
| Auth_group |
| auth_group_permissions |
| auth_permission |
| Auth_User |
| auth_user_groups |
| auth_user_user_permissions |
| blog2_blog2 |
| Blog3_blog3 |
| Blog4_blog4 |
| Blog_blog |
| Django_admin_log |
| Django_content_type |
| django_migrations |
| django_session |
+----------------------------+
Rows in Set (0.00 sec)

The table created by this synchronization database is BLOG4_BLOG4 and can be seen to be successful


(8) Manipulate the database to see if you can insert data
>python manage.py Shell
>>> from blog4.models import Blog4
>>> Blog4.objects.create (title= "Title 1", content= "Content 1")
<blog4:title 1>

Go to the database and look at:
Mysql> SELECT * from Blog4_blog4;
+----+---------+-----------+
| ID | Title | Content |
+----+---------+-----------+
| 1 | Title 1 | Content 1 |
+----+---------+-----------+
1 row in Set (0.00 sec)

Hehe, the insertion is successful, the work is done .....

Some of the steps are omitted and will be looked at

Reference connection: http://www.cnblogs.com/fengri/articles/django5.html

Django uses the MySQL database process

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.