In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "how to use Python descriptor to achieve ORM model", the content is detailed, the steps are clear, the details are handled properly, I hope this "how to use Python descriptor to achieve ORM model" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
Methods such as _ _ setattr__ cannot be triggered when accessing or modifying the properties of the descriptor object, only the _ _ set__,__get__,__delete__ method within the descriptor class.
ORM model: the class name corresponds to the table name, the data row corresponding to the object, and the class attribute corresponds to each field of the data row. If there are several table fields, several class attributes are bound; to add data to the table is to create an object, and each time you create an object, you add a row of data records.
The functions of the ORM framework:
1. The corresponding relationship between model classes and tables is established, which allows us to operate the database in an object-oriented way.
two。 Generate tables in the database according to the designed model class.
3. The database can be switched through convenient configuration.
Common data types of MySql:
1. Integer: int,bit
two。 Decimal: decimal (decimal for floating point, decimal (5 for 2) for total 5 digits, 2 decimal places)
3. String: varchar (variable length), char (immutable length)
4. Date time: date,time,datetime
5. Enumeration type: enum
Model class case:
ORM model field BooleanField for "" django: Boolean field, True or FalseCharField (maximum length of max_length=): string Parameter max_length indicates the maximum number of characters IntegerField: integer "" class TestReport (BaseTable): class Meat: verbose_name = 'test report' db_table = "TestReport" report_name = models.CharField (max_length=40, null=False) start_at = models.CharField (max_length=40) Null=True) status = models.BooleanField () testRun = models.IntegerField () successes = models.IntegerField () reports = models.TextField () 1.setting setting _ method: setting property "" as long as any one of the methods in a class appears: _ _ get__ (self,instance,owner) _ set__ (self,instance,value) _ delete__ (self,instance) _ set_name__ (self,owner,name) This class is not an ordinary class, but should be called: descriptor class (applied in the ORM model) "" class Field (object): "" as long as any of the following methods appear in a class This class is a descriptor class "" def _ get__ (self, instance, owner): pass def _ set__ (self, instance, value): print ('--set--- method is triggered') def _ delete__ (self, instance): passclass Model (object): attr = Field () # attr is a descriptor object Methods such as _ _ setattr__ cannot be triggered during modification. # can only trigger the _ _ set__ method if _ _ name__ = ='_ main__': m = Model () m.attr = 666 # within the descriptor class # try to modify the attr attribute print (m.attr) # output:-- set-method is triggered Noneclass Field (object): "" only one " Any of the following methods appear in the following classes This class is a descriptor class "" def _ get__ (self, instance, owner): pass def _ set__ (self, instance) Value): ": param instance: modified object: param value: modified value: return:"print ('--set--- method is triggered') self.value = value print (self) # print (instance) # print (value) # 666 def _ _ delete__ (self) Instance): passclass Model (object): attr = Field () # attr is a descriptor object Methods such as _ _ setattr__ cannot be triggered when modified. # can only trigger the _ _ set__ method if _ _ name__ inside the descriptor class ='_ _ main__': m = Model () print (m) # Same as instance m.attr = 666 # try to modify the attr property 2.Acess the property "" as long as any method in a class: _ _ get__ (self,instance,owner) _ set__ (self,instance,value) _ delete__ (self,instance) _ set_name__ (self,owner,name) appears in a class, this class is not a normal class. Should be called: descriptor class (applied in the ORM model) "" class Field (object): "" as long as any of the following methods appear in a class This class is a descriptor class "" def _ get__ (self, instance, owner): print ('--get--- method is triggered') def _ set__ (self, instance) Value): ": param instance: modified object: param value: modified value: return:"print ('--set--- method is triggered') self.value = value def _ delete__ (self, instance): passclass Model (object): attr = Field () # attr is a descriptor object Methods such as _ _ setattr__ cannot be triggered during modification. # can only trigger the _ _ set__ method if _ _ name__ = ='_ main__': m = Model () m.attr = 666 # within the descriptor class # try to modify the attr attribute print (m.attr) # output:-- set-method is triggered-get-- method is triggered None " "" as long as a class appears: _ _ get__ (self) Any method in instance,owner) _ set__ (self,instance,value) _ delete__ (self,instance) _ set_name__ (self,owner,name) This class is not an ordinary class, but should be called: descriptor class (applied in the ORM model) "" class Field (object): "" as long as any of the following methods appear in a class This class is a descriptor class "" def _ get__ (self, instance, owner): print ('--get--- method is triggered') print (instance) # print (owner) # return self.value def _ set__ (self, instance) Value): ": param instance: modified object: param value: modified value: return:"print ('--set--- method is triggered') self.value = value def _ delete__ (self, instance): passclass Model (object): attr = Field () # attr is a descriptor object Methods such as _ _ setattr__ cannot be triggered during modification. # can only trigger the _ _ set__ method if _ _ name__ = ='_ main__': m = Model () m.attr = 666 # within the descriptor class # try to modify the attr attribute print (m.attr) # 666 # output:-- set-method is triggered-get-- method is triggered
< __main__.Model object at 0x7f80b81a09d0 > < class '__main__.Model'>6663. Delete deleted _ method: delete attribute "" as long as any method in _ _ get__ (self,instance,owner) _ _ set__ (self,instance,value) _ delete__ (self,instance) _ set_name__ (self,owner,name) appears in a class, this class is not a normal class. Should be called: descriptor class (applied in the ORM model) "" class Field (object): "" as long as any of the following methods appear in a class This class is a descriptor class "" def _ get__ (self, instance, owner): return self.value def _ set__ (self, instance) Value): ": param instance: modified object: param value: modified value: return:"print ('--set--- method is triggered') self.value = value def _ _ delete__ (self) Instance): print ('--delete--- method is triggered') class Model (object): attr = Field () # attr is a descriptor object Methods such as _ _ setattr__ cannot be triggered during modification. # can only trigger the _ _ set__ method if _ _ name__ = ='_ _ main__': m = Model () m.attr = 666 # within the descriptor class # try to modify the attr attribute del m.attr #-delete--- method is triggered print (m.attr) # None # output:-set The method is triggered-delete-the method is triggered as long as it appears in a class: _ _ get__ (self) Any method in instance,owner) _ set__ (self,instance,value) _ delete__ (self,instance) _ set_name__ (self,owner,name) This class is not an ordinary class, but should be called: descriptor class (applied in the ORM model) "" class Field (object): "" as long as any of the following methods appear in a class This class is a descriptor class "" def _ get__ (self, instance, owner): print ('--get--- method is triggered') print (instance) # print (owner) # return self.value def _ set__ (self, instance) Value): ": param instance: modified object: param value: modified value: return:"print ('--set--- method is triggered') self.value = value def _ _ delete__ (self) Instance): print ('- delete--- method is triggered') print (instance) # self.value = Noneclass Model (object): attr = Field () # attr is a descriptor object Methods such as _ _ setattr__ cannot be triggered during modification. # can only trigger the _ _ set__ method if _ _ name__ = ='_ _ main__': m = Model () m.attr = 666 # within the descriptor class # try to modify the attr attribute del m.attr #-delete--- method is triggered print (m.attr) # None # output:-set Method triggered-delete-method triggered
< __main__.Model object at 0x7ff61806a160 >-get-method triggered
< __main__.Model object at 0x7ff61806a160 >None4. The descriptor implements the ORM model: "" implement the ORM model through the descriptor "" class CharField (object): def _ init__ (self,max_length=20): self.max_length = max_length def _ get__ (self, instance, owner): return self.value def _ set__ (self, instance) Value): # first determine whether it is empty if value is not None: # and then determine whether it is the string if isinstance (value, str): # and then determine whether the length meets the requirement if len (value)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.