当前位置:网站首页>ORM model -- creation and query of data records

ORM model -- creation and query of data records

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

1. Creation of database table records

        1.1 By model class name ( Field 1= value 1, Field 2= value 2...).save() The new record

1.2 By model class name .objects.create( Field 1= value 1, Field 2= value 2....) The new record

2. Query of database table records

        2.1 Query multiple data

                2.1.1 Query all data in the database

                        qs = Model class .objects.all(), What you get is a query set queryet object
                        a). Model class .objects.all(), return QuerySet object , Query set

                        b). Using model classes .objects.all(), All data in the data table corresponding to the current model class will be read out

                        c).QuerySet Similar list , It has the characteristics of lazy query , stay “ use ” Data Sql sentence

        2.2 Query single data

                2.2.1 Model class .objects.get()

                        This method is best to use fields with unique constraints to query

                        a). If you use get Query the number of records according to the specified conditions in =0 or >1, Will throw an exception

                        b). If you use get Query the number of records according to the specified conditions in =1, The model instance object of this record will be returned ,

                                 Can pass Instance object . Field name To get the value of the corresponding field

                2.2.2 Model class .objects.fillter()

                        a). You can use model classes .objects.filter( Conditions 1= value 1), return QuerySet object

                        b). If you use the specified criteria to query the number of records =0, Return empty QuerySet object

                        c). If you use the specified criteria to query the number of records >1, Wrap the qualified model object instances into QuerySet

                                 Object

                        d).QuerySet object , Similar list , Has the following characteristics :

                                Support positive integer index value

                                Support slicing operation

                                Get the first model object :QuerySet object .first()

                                 Get the last model object :QuerySet object .last()

                                To obtain the length of the :len(QuerySet object ), QuerySet object .count()

                                Judge query set QuerySet Whether the object is empty :QuerySet object .exists()

                                Support list iteration ,for Cycle, etc , Each cycle returns an instance of the model object

 

 

原网站

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