当前位置:网站首页>ORM -- logical relation and & or; Sort operation, update record operation, delete record operation

ORM -- logical relation and & or; Sort operation, update record operation, delete record operation

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

1. logical relationship

        1.1 And

                1.1.1 filter() Separate multiple filter conditions with commas in

                        qs = Projects.objects.filter(id='xx', full_name='xxx')

                1.1.2 Chain multiple calls filter() Method

                        qs = Projects.objects.filter(full_name='xxx').filter('id'='xx')

        1.2 or

                Use Q Class to specify or relate , For filtering conditions | Separate

                qs = Projects.objects.filter(Q(full_name__contains='2') | Q(leader='jeanet'))

2. The sorting operation

        QuerySet object .order_by("-id",'full_name')

        Use QuerySet object .order_by(‘ Field 1’,‘ Field 2’,‘- Field 3’), By default asc Sort , You can add ‘-’ To specify the desc Descending

3. Update record operation

        3.1 adopt obj. Field name =xxxx Pass after covering obj.save() Method     preservation , It is for the record to be fully updated

               adopt obj. Field name =xxxx Pass after covering obj.save(update_fields=[“ Field 1“,” Field 2“]) Method     preservation ,

                        Specify the updated fields , Is to update some fields

    3.2 adopt QuerySet object .update( Field name =‘xxx‘) Update multiple data

4. Delete record

        4.1 QuerySet object .delete(), Delete multiple records                

        4.2 obj.delete(), Delete a single record

 

               

原网站

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