1 What is ORM problem solving shortcomings ORM Object Relational Mapping relational mapping relationship analysis step first it did around this MySQL problem within 1 meter field is the type of Question 2, if a little more types how to do? Question 3. Assuming 100 table 100 need to write __init__ question 4: the number of fields of different field name of each table, resulting in not inherit directly Models parent problem takes a finite dictionary 4 / stored-value, in the hope to object attribute values. Object attribute = value stored mode value. Code implementation issue 5
What is ORM
problem
If we specific operation of the database, is not too much trouble, and every time dealing with complex SQL statements?
solve
A between Object-Relationl Mapping, role is to relational databases and objectMapping,
Like this we just need to operate it as usual as the operation target it. Greatly increases the development efficiency
Shortcoming
sql package fixed, not conducive to the SQL query optimization
Not one specific thing shows, I forwarded a specific look
ORM Object Relational Mapping
Relational Mapping
'' 'MySQL: python:
table name ----> class name
record ---> Object
Field ---> Object Properties' ''
Relationship Analysis
The above analysis of the relationship:
achieved produce a correspondence relationship a relational database table structure by creating a class in python
operated by the python class instantiated object operation creates a table recording a table of a database a corresponding relationship
to the operation record table corresponding value by way of a field attribute in python target point, the correspondence relationship between operation
step
First, it revolves around doing this MySQL
Creating a User table, in the table containing the fields (id, name, pwd),
class User : def __init__ ( self , id , name , pwd ) self . id = id self . name = name self . pwd = pwd
Table 1 is the problem of field type
But our table is the type of field, so it should be the first to write a field type class, imitation Youku system requires only two types, one is int, the other is varchar
Write field type class
Field name
Field type (length)
-varchar(64)
int
WhetherPrimary key
Defaults
The first to write int class
#需要先给字段3个默认值column_type,primary_key,default
class IntegerField:
def __init__(self,name,column_type='int',primary_key=False,default=None):
self.name=name
self.column_type=column_type
self.primary_key=primary_key
self.default=default
写完int.类再写string varchar()类 是不是觉得有点复杂了,
问题2,如果多一点的类型怎么办?
解决:
继承一个Models父类.
#父类
class Field:
def __init__(self,name,column_type,primary_key,default):
self.name=name
self.column_type=column_type
self.primary_key=primary_key
self.default=default
#两个字段类型类
#整型
class IntegerField:
def __init__(self,name,column_type='int',primary_key=False,default=None):
super().__init__(name,column_type,primary_key,default)
#字符串
class StringField:
def __init__(self,name,column_type='varchar(64)',primary_key=False,default=None):
super().__init__(name,column_type,primary_key,default)
仿优酷可以干些什么?
看电影,那么有个电影表类,还有公告表....
'''写表类
User:
user_name
pwd
Movie:
movie_name
movie_size
Notice:
title
content'''
问题3.假设100张表就需要写100个__init__
解决那么我们就写在一个父类里面 继承一个Models父类
但是又有问题了,即使我们想写父类也有限制,问题如下:
问题4:每张表的字段名与字段数量不同,导致没法直接继承Models父类
解决:
dict是对象,继承dict,触发字典内部的__init__(因为它具备可接受任意数量以及任意类型属性的特性),
验证:
原理解析:
dict是对象,(Python中一切皆为对象) 可以用来传任意关键字参数**kwargs ,形参中的**会将溢出的关键字实参全部接收,然后存储字典的形式,然后把字典赋值给**。key:value形式,可 自行查看源码
解决问题的方法总比 问题多,,
问题4 字典的取/存值方式有限,希望改为 对象.属性取值, 对象.属性=值 存值的方式。