Django models model ORM

I. Introduction of ORM

Mapping relations:

  Name of table

  Attributes of fields

  Table record – — class instantiated object

ORMThe two major functions are:

  Operating table:

    - Create a table

    - Modified table

    - Delete table

  Operation data row:

    - crud

ORMLink database with pymysql third party tools

DjangoThere is no way to help us create the database. We can only tell it after we have created it, and let Django link it.

Two. Preparation before creating the table.

First, create your own database.

Two, configure MySQL database link in settings

  sqlite3——Change to MySQL

Copy code ></span></div>
<pre># Modify Django the default database SQLite3 is mysql.DATABASES = {'default': {'ENGINE':'django.db.backendS.mysql', by using this link to link MySQL'NAME':'djangotsgl','USER':'root','PASSWORD':'123456','HOST':'localhost','PORT':'3306',}}    </pre>
<div class=Copy code ></span></div>
</div>
<p>  When you write this, Django will link to the database by default, and you will see the wrong report. The solution is as follows</p>
<p>Three. –init– files in app01</p>
<div class=
import pymysql
pymysql.install_as_MySQLdb()

Four, create database tables

models.py

Copy code ></span></div>
<pre>class Book(models.Model):  #Must be inheritedNID = models.AutoField (primary_key=True), where id is incrementally increased.Title = models.CharFieLD (max_length=32)PublishDdata = models.DateField ()Author = models.CharField (max_len)Gth=32)Price = models.DecimalField (max_digits=5, decimal_places=2), a total of 5 bits, retaining two decimal places.</pre>
<div class=Copy code ></span></div>
</div>
<p> Execution command creation: (need to remember!!!!  </p>
<div class=
python3 manage.py makemigrations   Create a scriptPython3 manage.py migrate migration

Specific examples to achieve

model.py

urls.py

views.py

template /index.html

Leave a Reply

Your email address will not be published. Required fields are marked *