Django models and ORM

Source: Internet
Author: User

ORM

Object Relational ing (ORM for short) is a technology designed to solve the problem of non-matching between objects and relational databases.

By using metadata that describes the ing between objects and databases, the objects in the program are automatically persisted to the relational database.

Advantages:

The main problem solved by ORM is the ing between objects and relationships. It usually maps a class to a table. each instance of the class corresponds to a record in the table, and each attribute of the class corresponds to each field in the table. Orm provides database ing. You do not need to write SQL code directly. You only need to operate on objects to operate data on the database. It enables software developers to focus on business logic processing and improves development efficiency.

Disadvantages:

The disadvantage of ORM is that it will sacrifice the execution efficiency of the program to a certain extent. Orm operations are limited, that is, operations defined by ORM can be completed, and some complex query operations cannot be completed.

Use the MySQL database in the Django Project

Set in settings

Databases = {"default": {"engine": "Django. DB. backends. mySQL "," name ":" Your Database Name ", # You need to manually create a database" user ":" database username "," password ":" Database Password ", "host": "database IP Address", "Post": 3306 }}

Write the following code in the _ init _. py file in the directory with the same name as the Django project to tell Django to connect to the MySQL database using the pymysql module:

import pymysql pymysql.install_as_MySQLdb()

Models

Model is a single and clear source of your data. It contains important fields and actions of the data you store. A model is usually mapped to a database table.

Below are the Mappings

 

Django models and ORM

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.