ORM多表操作

技术之博大精深 2020-06-01

from django.db import models

class Classes(models.Model):
    """
    班级表,男
    """
    titile = models.CharField(max_length=32)
    m = models.ManyToManyField(‘Teachers‘,related_name=‘sssss‘)

class Teachers(models.Model):
    """
    老师表,女
    """
    name = models.CharField (max_length=32)

class Student(models.Model):
    """
    学生表
    """
    username = models.CharField(max_length=32)
    age = models.IntegerField()
    gender = models.BooleanField()
    cs = models.ForeignKey(Classes,related_name=‘ssss‘) # cs,cs_id  1    3班



######################## 单表 ########################
# 增加
# Teachers.objects.create(name=‘root‘)
#或者:
# obj = Teachers(name=‘root‘)
# obj.save()
# 查
# Teachers.objects.all()
# Teachers.objects.filter(id=1)
# Teachers.objects.filter(id=1,name=‘root‘)
# result = Teachers.objects.filter(id__gt=1)
# [obj(id,name),]
# result = Teachers.objects.filter(id__gt=1).first()
# 删除
# Teachers.objects.filter(id=1).delete()
# 改
# Teachers.objects.all().update(name=‘alex‘)
# Teachers.objects.filter(id=1).update(name=‘alex‘)

######################## 一对多 ########################
"""
班级:
id    name
 1    3班
 2    6班

学生
id   username    age    gender   cs_id
1      成成       18     男        1
2      小芳       22     女        2
3      王杰       25     男        1
"""
# 增加
# Student.objects.create(username=‘成成‘,age=18,gender=‘男‘,cs_id=1)
# Student.objects.create(username=‘小芳‘,age=18,gender=‘女‘,cs= Classes.objects.filter(id=1).first() )
# 查看
"""
ret = Student.objects.all()
# []
# [ obj(..),]
# [ obj(1      成成       18     男        1),obj(2      小芳       22     女        2),obj(3      王杰       25     男        1),]
for item in ret:
    print(item.id)
    print(item.username)
    print(item.age)
    print(item.gender)
    print(item.cs_id)
    print(item.cs.id)
    print(item.cs.name)
"""
# 删除
# Student.objects.filter(id=1).delete()
# Student.objects.filter(cs_id=1).delete()

# cid = input(‘请输入班级ID‘)
# Student.objects.filter(cs_id=cid).delete()

# cname = input(‘请输入班级名称‘)
# Student.objects.filter(cs_id=cid).delete()
# Student.objects.filter(cs__name=cname).delete()  ##如果写filter跨表的时候不能用. 只能用__




######################## 多对多 ########################

# 多对多
"""
班级:
id  title
1    3班
2    4班
3    5班
老师:
id   title
 1    王老师
 2    陈老师
 3    李老师
 4    赵老师
 老师班级关系表(类):
id   班级id   老师id
 1     1        2
 2     1        3
 #3     1        4
 4     2        2
 5     2        3
 6     2        4
 7     1        5

# 增
obj = Classes.objects.filter(id=1).first() #1    3班
obj.m.add(2)  ##老师id=2
obj.m.add([4,3])

# obj = Classes.objects.filter(id=2).first() #1    3班
# obj.m.add(2)
# obj.m.add([4,3])

obj = Classes.objects.filter(id=1).first() #1    3班
# 删除
# obj.m.remove([4,3])
# 清空
obj.m.clear()
# 重置
obj.m.set([2,3,5])  ##不存在的新建

# 查第三张表
# 把3班的所有老师列举
obj = Classes.objects.filter(id=1).frist()
obj.id
obj.titile
ret = obj.m.all() # 第三张表
# ret是一个 [ 老师1(id,name),obj(id,name)   ]

"""
from django.shortcuts import render
from django.shortcuts import HttpResponse
from app01 import models

def test(request):
    # models.Classes.objects.create(titile=‘2班‘)
    # models.Classes.objects.create(titile=‘3班‘)
    # models.Classes.objects.create(titile=‘4班‘)
    #
    # models.Student.objects.create(username=‘王成‘,age=24,gender=True,cs_id=1)
    # models.Student.objects.create(username=‘王杰‘, age=22, gender=True, cs_id=1)
    # models.Student.objects.create(username=‘王忠‘,age=18,gender=True,cs_id=1)
    # models.Student.objects.create(username=‘王浩‘,age=33,gender=True,cs_id=2)

    # ret = models.Student.objects.filter(cs__titile=‘2班‘)
    # print(ret)

    # obj = models.Classes.objects.filter(titile=‘2班‘).first()
    # print(obj.id)
    # print(obj.titile)
    ####反向查找  反向:小写类名_set(默认) ==> related_name=‘ssss‘
    ##print(obj.student_set.all())
    # print(obj.ssss.all())

    # ret = models.Classes.objects.all().values(‘id‘, ‘titile‘,‘ssss‘,"ssss__username")
    # print(ret)
    # # 注意谁是主表
    # models.Student.objects.all().values(‘username‘, ‘cs__titile‘)  ##学生为主表
    # models.Classes.objects.all().values(‘titile‘, ‘ssss__username‘)  ##班级为主表


    # #################### 多对多 ####################
    # models.Teachers.objects.create(name=‘王老师‘)
    # models.Teachers.objects.create(name=‘陈老师‘)
    # models.Teachers.objects.create(name=‘李老师‘)
    # models.Teachers.objects.create(name=‘赵老师‘)

    # cls_list = models.Classes.objects.all()  ##找出所有班级obj.id obj.title obj.m.all()
    # for obj in cls_list:
    #     print(obj.id,obj.titile)
    #     for row in obj.m.all():
    #         print(‘----‘,row.name)

    # obj = models.Classes.objects.filter(id=1).first()
    # obj.m.add(3)  ##给id=1的班级新增一个老师

    # obj = models.Teachers.objects.filter(id=2).first()
    # obj.sssss.set([1,2]) ##给id=2的老师重置id=1,2的班级
    # obj.sssss.remove(3) ##删除


    # v = models.Classes.objects.all().values(‘id‘,‘titile‘,‘m‘,‘m__name‘)
    # print(v)
    # v = models.Classes.objects.all().values(‘id‘,‘titile‘,‘m‘,‘m__name‘) ##字典
    
    # print(v)
    # for item in v:
    #     print(item[‘m‘],type(item[‘m‘]))

    # v = models.Teachers.objects.all().values(‘name‘,‘sssss__titile‘)
    # print(v)
    return HttpResponse(‘...‘)

相关推荐

inspuryhq / 0评论 2020-07-28