一、ORM
ORM:Object Relational Mapping(关系对象映射)
类名对应------》数据库中的表名
类实例对应---------》数据库表里的一行数据
类属性对应---------》数据库里的字段
obj.id obj.name.....类实例对象的属性
二、ORM操作表
1.单表
class UserInfo(models.Model): """ 用户表 """ username = models.CharField(verbose_name='用户名', max_length=32)
2.ForeignKey
class Department(models.Model): """ 部门表 """ title = models.CharField(verbose_name='标题',max_length=32) class UserInfo(models.Model): """ 用户表 """ username = models.CharField(verbose_name='用户名', max_length=32) depart = models.ForeignKey(verbose_name='所属部门',to="Department")
on_delete: models.CASCADE,删除部门,则将改部门下的员工全部删除。 + 代码判断 models.DO_NOTHING,删除部门,引发错误IntegrityError models.PROTECT,删除部门,引发错误ProtectedError models.SET_NULL,删除部门,则将改部门下的员工所属部门ID设置为空。(将FK字段设置为null=True) models.SET_DEFAULT,删除部门,则将改部门下的员工所属部门ID设置默认值。(将FK字段设置为default=2) models.SET,删除部门,则将执行set对应的函数,函数的返回值就是要给改部门下员工设置的新的部门ID。 例如: def func(): models.Users....... return 10 class MyModel(models.Model): user = models.ForeignKey(to="User",to_field="id"on_delete=models.SET(func),) 方法: models.CASCADE, 删除逻辑时,通过代码判断当前 “部门” 下是否有用户。 models.SET_NULL,稳妥。 沟通之后在确定。
depart = models.ForeignKey(verbose_name='所属部门',to="Department",db_constraint=False) # 无约束,但可以使用django orm的连表查询。 models.UserInfo.objects.filter(depart__title='xxx')
示例1: from django.db import models class Department(models.Model): """ 部门表 ID 名称 1 教质部 2 Python学院 """ title = models.CharField(verbose_name='标题',max_length=32) class User(models.Model): """ 员工表 ID name depart_id 1 小雪 1 2 冰冰 1 3 小雨 1 4 太亮 2 5 金菊 2 """ name = models.CharField(verbose_name='员工名称',max_length=32) depart = models.ForeignKey(to='Department') class ClassList(models.Model): """ 班级表 """ title = models.CharField(verbose_name='班级名称', max_length=32) bzr = models.ForeignKey(to=User,limit_choices_to={'id__lt':4}) teacher = models.ForeignKey(to=User,limit_choices_to={'id__gte':4}) 示例2: from django.db import models class Department(models.Model): """ 部门表 ID 名称 1 教质部 2 Python学院 """ title = models.CharField(verbose_name='标题',max_length=32) class User(models.Model): """ 员工表 ID name depart_id 1 小雪 1 2 太亮 2 3 小雨 1 4 冰冰 1 5 金菊 2 """ name = models.CharField(verbose_name='员工名称',max_length=32) depart = models.ForeignKey(to='Department') class ClassList(models.Model): """ 班级表 """ title = models.CharField(verbose_name='班级名称', max_length=32) bzr = models.ForeignKey(to=User,limit_choices_to={'depart__title':'教质部','id__gt':9}) teacher = models.ForeignKey(to=User,limit_choices_to={'depart__title':'Python学院'})
反向查找的字段。 示例: from django.db import models class Department(models.Model): """ 部门表 ID 名称 1 教质部 2 Python学院 """ title = models.CharField(verbose_name='标题',max_length=32) class User(models.Model): """ 员工表 ID name depart_id 1 小雪 1 2 太亮 2 3 小雨 1 4 冰冰 1 5 金菊 2 """ name = models.CharField(verbose_name='员工名称',max_length=32) depart = models.ForeignKey(to='Department') class ClassList(models.Model): """ 班级表 """ title = models.CharField(verbose_name='班级名称', max_length=32) bzr = models.ForeignKey(to=User,related_name='x') teacher = models.ForeignKey(to=User,related_name='y') from app01 import models # 找班主任小雪带的所有班级 obj = models.User.objects.filter(name='小雪').first() class_list = obj.x.all() for row in class_list: print(row.title) # 找老师金鑫带的所有班级 obj1 = models.User.objects.filter(name='金鑫').first() class_list = obj1.y.all() for row in class_list: print(row.title)
对于ForeignKey,一般公司数据量和访问量不大时,创建FK做约束。数据量和访问量巨大时,牺牲硬盘空间和程序员代码量,依次来提供用户访问速度。(连表查询速度会比单表查询速度慢)
3.ManyToMany
class Boy(models.Model): name = models.CharField(max_length=32) class Girl(models.Model): name = models.CharField(max_length=32) boy = models.ManyToManyField('Boy')
class Boy(models.Model): name = models.CharField(max_length=32) class Girl(models.Model): name = models.CharField(max_length=32) class Boy2Girl(models.Model): b = models.ForeignKey(to='Boy') g = models.ForeignKey(to='Girl') class Meta: unique_together = ( ("b", "g"), )
4.OneToOne
class UserInfo(models.Model): """ 1 好虚 2 戴绿 """ username = models.CharField(verbose_name='标题',max_length=32) class Blog(Model.Model): """ 1 好虚371 1 """ title = models.CharField(verbose_name='标题',max_length=32) a = models.OneToOneField(to='A') 应用场景: class userinfo: """ 老男孩所有员工 (130) """ name = 用户名 email = 邮箱 ... class Admin: """ 给30个人开账号(30),可以登录教务系统 """ username = 登录用户名 password ='密码' user = o2o(userinfo)
例如:性别的数量不会随着时间的推移而发生个数的变化。 # 不推荐 class Gender(models.Model): title = models.CharField(max_length=32) class Customer(models.Model): name = models.CharField(verbose_name='姓名',max_length=32) gender = models.ForeignKey(to='Gender') # 推荐 class Customer(models.Model): name = models.CharField(verbose_name='姓名',max_length=32) gender_choices = ( (1,'男'), (2,'女'), ) gender = models.IntegerField(choices=gender_choices) 数据库优化手段,将固定数据放入内存代替放入数据库。
三、ORM操作数据
class Department(models.Model): title = models.CharField(verbose_name='标题',max_length=32) class UserInfo(models.Model): name = models.CharField(verbose_name='员工名称',max_length=32) depart = models.ForeignKey(to='Department') roles = models.ManyToManyField(to="Role") class Role(models.Model): title = models.CharField(verbose_name='标题',max_length=32) 增加: models.Department.objects.create(title='销售部') models.Department.objects.create(**{'title':'销售部'}) models.UserInfo.objects.create(name='刘也',depart=models.Department.objects.get(id=1)) models.UserInfo.objects.create(name='刘也',depart_id=1) obj = models.UserInfo.objects.filter(name='刘也').first() obj.roles.add([1,2,3]) 删除: .delete() 修改: models.UserInfo.objects.filter(id__gt=5).update(name='xx') obj = models.UserInfo.objects.filter(name='刘也').first() obj.roles.set([2,3,6,7]) 查询: models.UserInfo.objects.all() models.UserInfo.objects.values('id','name') models.UserInfo.objects.values_list('id','name')
- 排序 - 连表 - filter筛选条件 __gt __gte __lt __contains __in ...
F Q only # Queryset[obj,obj,obj] modes.UserInfo.objects.all().only('id','name') # select id,name from userinfo # Queryset[{},{},{}] modes.UserInfo.objects.all().values('id','name') # select id,name from userinfo # Queryset[(),(),()] modes.UserInfo.objects.all().values_list('id','name') # select id,name from userinfo 错错错: result = modes.UserInfo.objects.all().only('id','name') for obj in result: print(obj.id,obj.name,obj.age) defer # Queryset[obj,obj,obj] modes.UserInfo.objects.all().defer('name') # select id,age from userinfo select_related 帮助开发者进行主动连表查询。 # SELECT "app01_user"."id", "app01_user"."name", "app01_user"."depart_id" FROM "app01_user" result = models.User.objects.all() # SELECT "app01_user"."id", "app01_user"."name", "app01_user"."depart_id", "app01_department"."id", "app01_department"."title" FROM "app01_user" INNER JOIN "app01_department" ON ("app01_user"."depart_id" = "app01_department"."id") result = models.User.objects.all().select_related('depart') 注意:如果以后想要获取部门名称(跨表),一定要使用select_related进行主动跨表,这样在最开始获取数据时,将当前表和关联表的所有数据都获取到。 切记:错错错 result = models.User.objects.all() for row in result: print(row.name,row.depart_id,row.depart.title) # row.depart.title就会让性能大大降低 prefetch_related # 先执行SQL: select * from user where id<100 # 在执行SQL: select * from depart where id in [11,20] result = models.User.objects.filter(id__lt=100).prefetch_related('depart') 对比: 方式一: result = models.User.objects.all() # 1次单表 for row in result: print(row.id,row.name,row.depart.title) # 100次单表 方式二(小于4张表的连表操作): *** result = models.User.objects.all().select_related('depart') # 1次连表查询 for row in result: print(row.id,row.name,row.depart.title) 方式三(大于4张表连表操作): # 先执行SQL: select * from user; # 在执行SQL: select * from depart where id in [11,20] result = models.User.objects.all().prefetch_related('depart') # 2次单表查询 for row in result: print(row.id,row.name,row.depart.title) 执行原生SQL,场景:复杂SQL语句 from django.db import connection, connections # cursor = connections['db1'].cursor() cursor = connection.cursor() cursor.execute("""SELECT * from auth_user where id = %s""", [1,]) # row = cursor.fetchall() # 获取符合条件的所有数据,models.User.objects.all() row = cursor.fetchone() # 获取符合条件的第一条数据,models.User.objects.all().first()
################################################################## # PUBLIC METHODS THAT ALTER ATTRIBUTES AND RETURN A NEW QUERYSET # ################################################################## def all(self) # 获取所有的数据对象 def filter(self, *args, **kwargs) # 条件查询 # 条件可以是:参数,字典,Q def exclude(self, *args, **kwargs) # 条件查询 # 条件可以是:参数,字典,Q def select_related(self, *fields) 性能相关:表之间进行join连表操作,一次性获取关联的数据。 model.tb.objects.all().select_related() model.tb.objects.all().select_related('外键字段') model.tb.objects.all().select_related('外键字段__外键字段') def prefetch_related(self, *lookups) 性能相关:多表连表操作时速度会慢,使用其执行多次SQL查询在Python代码中实现连表操作。 # 获取所有用户表 # 获取用户类型表where id in (用户表中的查到的所有用户ID) models.UserInfo.objects.prefetch_related('外键字段') from django.db.models import Count, Case, When, IntegerField Article.objects.annotate( numviews=Count(Case( When(readership__what_time__lt=treshold, then=1), output_field=CharField(), )) ) students = Student.objects.all().annotate(num_excused_absences=models.Sum( models.Case( models.When(absence__type='Excused', then=1), default=0, output_field=models.IntegerField() ))) def annotate(self, *args, **kwargs) # 用于实现聚合group by查询 from django.db.models import Count, Avg, Max, Min, Sum v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id')) # SELECT u_id, COUNT(ui) AS `uid` FROM UserInfo GROUP BY u_id v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id')).filter(uid__gt=1) # SELECT u_id, COUNT(ui_id) AS `uid` FROM UserInfo GROUP BY u_id having count(u_id) > 1 v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id',distinct=True)).filter(uid__gt=1) # SELECT u_id, COUNT( DISTINCT ui_id) AS `uid` FROM UserInfo GROUP BY u_id having count(u_id) > 1 def distinct(self, *field_names) # 用于distinct去重 models.UserInfo.objects.values('nid').distinct() # select distinct nid from userinfo 注:只有在PostgreSQL中才能使用distinct进行去重 def order_by(self, *field_names) # 用于排序 models.UserInfo.objects.all().order_by('-id','age') def extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None) # 构造额外的查询条件或者映射,如:子查询 UserInfo.objects.extra(where=['headline ? %s'], params=['Lennon']) # select * from userinfo where headline > 'Lennon' UserInfo.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"]) # select * from userinfo where (foo='a' OR bar = 'a') and baz = 'a' UserInfo.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,)) """ select id, name, (select col from sometable where othercol > 1) as new_id """ UserInfo.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid']) def reverse(self): # 倒序 models.UserInfo.objects.all().order_by('-nid').reverse() # 注:如果存在order_by,reverse则是倒序,如果多个排序则一一倒序 def defer(self, *fields): models.UserInfo.objects.defer('username','id') 或 models.UserInfo.objects.filter(...).defer('username','id') #映射中排除某列数据 def only(self, *fields): #仅取某个表中的数据 models.UserInfo.objects.only('username','id') 或 models.UserInfo.objects.filter(...).only('username','id') def using(self, alias): 指定使用的数据库,参数为别名(setting中的设置) models.UserInfo.objects.filter(id=5).using('db1') ################################################## # PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS # ################################################## def raw(self, raw_query, params=None, translations=None, using=None): # 执行原生SQL models.UserInfo.objects.raw('select * from userinfo where id > 10 ') # 如果SQL是其他表时,必须将名字设置为当前UserInfo对象的主键列名 models.UserInfo.objects.raw('select id as nid from 其他表') # 为原生SQL设置参数 models.UserInfo.objects.raw('select id as nid from userinfo where nid>%s', params=[12,]) # 将获取的到列名转换为指定列名 name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'} Person.objects.raw('SELECT * FROM some_other_table', translations=name_map) # 指定数据库 models.UserInfo.objects.raw('select * from userinfo', using="default") ################### 原生SQL ################### from django.db import connection, connections cursor = connection.cursor() # cursor = connections['default'].cursor() cursor.execute("""SELECT * from auth_user where id = %s""", [1]) row = cursor.fetchone() # fetchall()/fetchmany(..) def values(self, *fields): # 获取每行数据为字典格式 def values_list(self, *fields, **kwargs): # 获取每行数据为元祖 def dates(self, field_name, kind, order='ASC'): # 根据时间进行某一部分进行去重查找并截取指定内容 # kind只能是:"year"(年), "month"(年-月), "day"(年-月-日) # order只能是:"ASC" "DESC" # 并获取转换后的时间 - year : 年-01-01 - month: 年-月-01 - day : 年-月-日 models.DatePlus.objects.dates('ctime','day','DESC') def datetimes(self, field_name, kind, order='ASC', tzinfo=None): # 根据时间进行某一部分进行去重查找并截取指定内容,将时间转换为指定时区时间 # kind只能是 "year", "month", "day", "hour", "minute", "second" # order只能是:"ASC" "DESC" # tzinfo时区对象 models.DDD.objects.datetimes('ctime','hour',tzinfo=pytz.UTC) models.DDD.objects.datetimes('ctime','hour',tzinfo=pytz.timezone('Asia/Shanghai')) """ pip3 install pytz import pytz pytz.all_timezones pytz.timezone(‘Asia/Shanghai’) """ def none(self): # 空QuerySet对象 #################################### # METHODS THAT DO DATABASE QUERIES # #################################### def aggregate(self, *args, **kwargs): # 聚合函数,获取字典类型聚合结果 from django.db.models import Count, Avg, Max, Min, Sum result = models.UserInfo.objects.aggregate(k=Count('u_id', distinct=True), n=Count('nid')) ===> {'k': 3, 'n': 4} def count(self): # 获取个数 def get(self, *args, **kwargs): # 获取单个对象 def create(self, **kwargs): # 创建对象 def bulk_create(self, objs, batch_size=None): # 批量插入 # batch_size表示一次插入的个数 objs = [ models.DDD(name='r11'), models.DDD(name='r22') ] models.DDD.objects.bulk_create(objs, 10) def get_or_create(self, defaults=None, **kwargs): # 如果存在,则获取,否则,创建 # defaults 指定创建时,其他字段的值 obj, created = models.UserInfo.objects.get_or_create(username='root1', defaults={'email': '1111111','u_id': 2, 't_id': 2}) def update_or_create(self, defaults=None, **kwargs): # 如果存在,则更新,否则,创建 # defaults 指定创建时或更新时的其他字段 obj, created = models.UserInfo.objects.update_or_create(username='root1', defaults={'email': '1111111','u_id': 2, 't_id': 1}) def first(self): # 获取第一个 def last(self): # 获取最后一个 def in_bulk(self, id_list=None): # 根据主键ID进行查找 id_list = [11,21,31] models.DDD.objects.in_bulk(id_list) models.User.objects.filter(id__in=[11,21,31]) def delete(self): # 删除 def update(self, **kwargs): # 更新 def exists(self): # 是否有结果