Initial knowledge of Django's ORM arrangement and the increase and deletion search

Source: Internet
Author: User

1. Database driver: Download the driver for database--->data source. 2. Database toolkit: py2.0 support MySQLdb (), but py3 in MySQLdb () is not fully applicable, need to be replaced, after all, the ORM is based on the database operation of the PY Toolkit, please ensure that you have downloaded the Pymysql, under the project folder __ init__.py add: Import Pymysql pymysql.install_as_mysqldb () 3. Database connection settings: Configure database connection information in settings.py: DATABASES = {' default ': {# ' Engine ': ' Django.db.backends.sqlite3 ', # ' NAME ': Os.path.join (Base_dir, ' db.sqlite3 '), these two lines implicitly go to Django's default configuration ' engine ': ' Django.db.backends.mysql ', ' NAME ': ' Django_orm ', ' USER ': ' Root ', ' PASSWORD ': ' song6748106 ', ' HOST ': ', #不填默认本机 ' PORT ': ' 3306 ',}} Here you need to note that Name,password, etc. need to be capitalized, otherwise not connected, very pit. The above configuration is basically complete, then the database is started. 1. In the models.py definition class, the map table structure, where the from django.db import models is required to introduce the models package. (Terminal input Python manage.py make migirations, that is, the makemigrations instruction is used to compare the model in models and the model in the current migrations code. If there is a new modification, the Py file record is created. 2. Route configuration, open urls.py new route. 3. View configuration, introduce Model,from app.models import * and delete and change under View function. Add the database record via ORM (default table Author): A1=author (id=,name= "", sex= "", ") #实例了models中的类 (a table in the mapping database), field to correspond, must save, Save is an all-field update with low efficiency. A1.save () way two: Author.objects.creAte (id=2,name= "Cao Xueqin", sex= "female",) the database records are deleted via ORM: Author.objects.filter (name= "Cao Xueqin"). Delete () modifying database records via ORM: Author.objects.filter (name= ""). Update (sex= "") mode Two: A=author.objects.get (name= "Wu Chengen") # Get must be a record, filter takes a Queryset collection (not applicable to 0 index value), multiple modifications please use one. a.sex= "female" a.save ()

Knowledge of Django's ORM placement and deletion

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.