Django Connection Database requirements: 1) pycharm run the Django platform; 2) MySQL database. The premise of this article is that all two platform readers have been installed correctly, not installed friends please Baidu. The Django I used is version 2.1, which differs from the old version when connecting data. The Pycharm feature is very powerful and all MySQL command operations can be implemented in Pycharm.
First, Pycharm connection database
Find the MySQL data source
Set the user name and password to connect to the database, typically the root user
Once set up, you can view the contained database by command-line operation
Database display
In the future, all related operations on the data can be operated in Pycharm, which is very convenient.
Second, the database connection configuration
python2.x series Connection MySQL database needs MySQLdb class library support, python3.x series connection MySQL need Pymysql class library support,
python3.6 Mounting Module Pymysql:
1. Command line install pip install Pymysql,
2. Use the Pycharm supported Class library installation, the installation process and the Django installation process in two ways
3. Also need to install PIP install Mysqlclient, so that the import MySQLdb will not error
See if the installation package is installed in Pycharm
OK, everything is well, start coding.
Create a database
Making a database connection in settings.py
Write the database logic in models.py, where the class name corresponds to the table in the database, giving only one property as a demonstration
Next, build the table structure and enter in the terminal:
View on the database side
To view the tables in the database Deng
From the results, we can see that there are many tables, but our own is only the Sayhello_employee table established at that time. Let's look at the information in this table again:
In the table, when the Django Orm creates a table, an ID field (default, primary key, autogrow), a Name field, is defined in the models.py, and the maximum length is 30, which is reflected
The first section of the database operation is complete
Django-mysql database uses a