<>ORM Add, delete, modify and check

Two ways to add

* save()
* Save through model class

be careful : Add information with foreign keys. The operation is as follows : herozhu = HeroInfo.objects.create( hname = " Zhu Bajie ", hgender = 0,
hdesc= 'houhouhou', hbook = book ) herosha = HeroInfo.objects.create( hname =
" Sha Wujing ", hgender = 1, hdesc = ' ha-ha ', hbook = book )

Modify data
There are two ways to modify and update
1) Modify the properties of model class objects , Then execute save() method

2) Using model classes .objects.filter().update(), The number of rows affected is returned

At this time, the database contents are modified successfully

Delete data
There are two ways to delete
1) Model class object delete

At this time, the database content :

2) Model class .objects.filter().delete()

At this time, the database content :

Basic query
usage method :
get: Query single result , If it does not exist, it will be thrown DoesNotExist abnormal , If there are multiple results, it will be reported MultipleObjectsReturned abnormal all
: Query multiple results count: Number of query results

Filter query
When querying __ ( Double underline ) Express judgment

* filter: Filter out multiple results
* exclude: Exclude the remaining results that meet the conditions
* get: Filter single results
* exact: Indicates equal judgment
* contains: Indicates whether to include
* startswith and endswith: Start or end with the specified
* isnull: Is it null
* exclude: filter
* in: Is it included in the scope
* gt: greater than
* gte: Greater than or equal to
* lt: less than
* lte: Less than or equal to

F and Q object
1)F:
The previous queries are the comparison of object attributes and constant values , How are the two attributes compared ? answer : use F object , Defined in django.db.models in .
grammar :F( Attribute name ) Guide Package :from django.db.models import F,Q

2)Q:

Multiple filters call one by one to represent logic and relationships , with sql In the statement where Partial and keyword .
realization mysql Inside not and or

Aggregate and sort functions
1) Aggregate function :

use aggregate() Filter calls aggregate function . Aggregate functions include :Avg average ,Count quantity ,Max maximum ,Min minimum ,Sum Sum , Defined in django.db.models in .

sort :
use order_by Sort results

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage MySQL Basics Commonly used sentence ( Add / delete / modify query )C Language of a Gobang game implementation QT5.9 Use of learning notes QSqlQuery Method of Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control java Polymorphic array of web Front end signature plug-in _signature_pad Plug in implements electronic signature function centos7 install RabbitMqspringboot use redis Experiment 4 Automated test tools - software test