How to implement a simple ORM Model with Python
This article mainly introduces "how to use Python to achieve a simple ORM model". In daily operation, I believe many people have doubts about how to use Python to achieve a simple ORM model. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use Python to achieve a simple ORM model". Next, please follow the editor to study!
Metaclass
As for metaclass, my understanding is actually relatively shallow, which is probably what it means.
All classes are created using metaclasses, and the parent class of all classes must be object (for Python3). There is only one metaclass in Python (type). Of course, there is no custom metaclass here.
Let's take a look at the creation of classes.
Class Test: # define a class passTest1 = type ("Test2", (object,), {"name": "test"}) # define a class print (type (Test)) print (type (Test1))--
As can be seen from the above, there are actually two ways to create a class, one is to define it through the class keyword, the other is to create it through type, of course, it is commonly used to create a class. Looking at the final result, you can see that the type of the class is type. It means that our class is created by type.
After understanding this, let's sort out how to use custom metaclasses to create classes. To understand, custom metaclasses need to inherit type.
Class MetaClass (type): # define a metaclass passclass Test (metaclass=MetaClass): # create a class passprint (type (Test)) using a custom metaclass
It is obvious that the Test class was created with the MetaClass class
Descriptor
From the definition of a descriptor, as long as one or more of _ _ get__, _ _ set__, _ _ delete__ are implemented in a class, an instance of this class can be called a descriptor.
Let's define a simple descriptor
Class Describer: def _ _ set__ (self, instance, value): print ("called when setting properties") self.value = value def _ _ get__ (self, instance, owner): print ("called when getting properties") return self.value def _ _ delete__ (self Instance): print ("called when deleting an attribute") self.value = Noneclass Test: name = Describer () t = Test () t.name = "xxxxx" print (t.name)-- xxxxx is called when the property is called to get the property
Do you have any ideas from the above code? Since the _ _ set__ method will be called when we set the property, can we do something with the property before setting it?
ORM model
What on earth is the ORM model? ORM is certainly no stranger to back-end research and development, including many back-end frameworks now come with this model.
ORM (Object Relational Mapping) object-relational mapping
Since it is an object-relational mapping, what is the object? My understanding is: for the mapping between the classes in Python and the database, there is no need to write SQL for the operation of the data, because it is all encapsulated. For example, if you want to insert a piece of data, you can create an object directly.
Python-> Database
Class name-> Table name in the database
Object-> A row of data in a database
Properties-> Fields in the database
It's roughly the mapping relationship above.
ORM implementation steps
1. Use the descriptor to realize the restriction on the type and length of database fields.
2. Implement the Mode class, that is, the class in Python
3. Use metaclass to realize the mapping relationship.
Okay, let's first use the descriptor to limit the type and length of the data field.
Class BaseFiled: passclass CharFiled (BaseFiled): "" define the type restriction of a string "" def _ _ init__ (self, length=10): self.length = length def _ _ set__ (self, instance, value): if isinstance (value, str): if len (value)