当前位置:网站首页>ORM -- grouping query, aggregation query, query set queryset object properties

ORM -- grouping query, aggregation query, query set queryset object properties

2022-07-07 10:07:00 chuntian_ tester

1. Group query

        1.1. Use  QuerySet object .values(‘ Parent table primary key id’).annotate( Aggregate functions (‘ From the table model, the class name is lowercase ')

        1.2. The master-slave table will be automatically connected , Take the foreign key field as the grouping condition

 

2. Aggregate operations

        2.1. Use QuerySet object .aggregate( Aggregate functions (' Field name ')) Method , Return dictionary data

        2.2. In the returned dictionary data key by Field name __ Aggregate function name A lowercase letter

        2.3. Key parameter form can be used , In the returned dictionary data key Is the keyword parameter name

                qs = Projects.objects.filter(full_name__contains='x').aggregate(Count("id")) 

                qs = Projects.objects.filter(full_name__contains='x').aggregate(count=Count("id"))

3. Query set QuerySet Properties of objects :

        3.1 Support chain calls

                You can call... Multiple times on a query set filter(),exclude() Method

        3.2 Lazy query

                Execute only when using data Sql sentence , In order to improve the read and write performance of the database

        3.3 Will execute Sql Statement scenario :

                len(),count(), Take value through index , section ,for loop ,print etc.

原网站

版权声明
本文为[chuntian_ tester]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070717235359.html