Python Learning---Django model syntax 180124

Source: Internet
Author: User

Django model syntax [Models]

1 Django supports Sqlite,mysql, Oracle,postgresql database by default.

<1> SQLite

Django uses SQLite's database by default, default with SQLite database driver, engine name: Django.db.backends.sqlite3

<2> MySQL

Engine Name: Django.db.backends.mysql

2 MySQL Driver

MySQLdb (MySQL python)

Mysqlclient

Mysql

Pymysql (pure python mysql driver)

Change the default database to MySQL

setttings.py

DATABASES = {'    default ': {        ' ENGINE ': ' Django.db.backends.mysql ',         ' name ': ' Books ',      # Your database name         ' User ': ' Root ',       # your database username         ' PASSWORD ': ',       # Your database password         ' host ': ',           # Your database host, leave blank default to localhost        ' Port ': ' 3306 ',       # Your database port    }}

Attention:

Name is the name of the database, the database must have been created before the MySQL connection, and the db.sqlite3 under the SQLite database above is automatically created by the project

User and password are the username and password of the database respectively.

Once setup is complete, we need to activate our MySQL before starting our Django project.

Then, start the project and will error: no module named MySQLdb

This is because Django defaults to the driver you are importing is MYSQLDB, but MySQLdb is a big problem for py3, so the driver we need is pymysql, that is, we just need to find the __init__ under the project name file and write it inside:

Import Pymysql

Pymysql.install_as_mysqldb ()

Problem Solving!

Python Learning---Django model syntax 180124

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.