|
|
|
from orm_demo.models import *
|
|
|
|
|
|
|
|
# 插入数据
|
|
|
|
# 先实例化对象,然后为对象赋值
|
|
|
|
depart_data = DepartInfo()
|
|
|
|
depart_data.depart_name = '开发部'
|
|
|
|
depart_data.save()
|
|
|
|
|
|
|
|
# 实例化对象同时赋值
|
|
|
|
skill_data = SkillInfo(skill_name='PHP')
|
|
|
|
skill_data.save()
|
|
|
|
|
|
|
|
# create方法赋值
|
|
|
|
user = BaseUserInfo.objects.create(username='张三', password='zhangsan', status=1)
|
|
|
|
|
|
|
|
# create方式赋值, 字典格式数据
|
|
|
|
data = {'username': '李四', 'password': 'lisi', 'status': 1}
|
|
|
|
# data2 = dict(username='李四', password='lisi', status=1) # 另一种字典数据定义形势
|
|
|
|
user2 = BaseUserInfo.objects.create(**data)
|
|
|
|
|
|
|
|
# 批量插入
|
|
|
|
data_list = [
|
|
|
|
{'username': '王五', 'password': 'wangwu', 'status': 1},
|
|
|
|
{'username': '孙六', 'password': 'sunliu', 'status': 1}
|
|
|
|
]
|
|
|
|
user_obj_list = []
|
|
|
|
for data in data_list:
|
|
|
|
user_obj = BaseUserInfo(username=data['username'], password=data['password'], status=data['status'])
|
|
|
|
user_obj_list.append(user_obj)
|
|
|
|
users = BaseUserInfo.objects.bulk_create(user_obj_list)
|
|
|
|
|
|
|
|
# 插入外键关联的数据
|
|
|
|
user1 = BaseUserInfo.objects.filter(username='张三').first()
|
|
|
|
department1 = DepartInfo.objects.filter(depart_name='开发部').first()
|
|
|
|
user_e1 = UserExtendInfo(username='张三', true_name='张三丰', sex=1, age=33, department='开发部', salary='230000.00',
|
|
|
|
status=1, memo='', user=user1, depart=department1)
|
|
|
|
user_e1.save()
|
|
|
|
|
|
|
|
# 插入ManyToMany字段数据add()和set()
|
|
|
|
skill_php = SkillInfo.objects.filter(skill_name='PHP').first()
|
|
|
|
user_zhang = UserExtendInfo.objects.filter(username='张三').first()
|
|
|
|
skill_php.user.add(user_zhang) # 使用add方法添加外键关联关系
|
|
|
|
|
|
|
|
# 一次建立多个连接关系
|
|
|
|
skill_py = SkillInfo.objects.filter(skill_name='Python').first()
|
|
|
|
user_zhang = UserExtendInfo.objects.filter(username='张三').first()
|
|
|
|
user_sun = UserExtendInfo.objects.filter(username='孙六').first()
|
|
|
|
skill_py.user.add(user_zhang, user_sun) # add方法可以添加多个关联关系
|
|
|
|
|
|
|
|
# 直接创建关联关系数据
|
|
|
|
skill_draw = SkillInfo.objects.filter(skill_name='draw').first()
|
|
|
|
user_zhou = BaseUserInfo.objects.filter(username='周七').first()
|
|
|
|
depart_zhou = DepartInfo.objects.filter(depart_name='设计部').first()
|
|
|
|
skill_draw.user.create(username='周七', true_name='周七天', sex=0, age=18, department='设计部', salary='8500.00',
|
|
|
|
status=1, memo='', user=user_zhou, depart=depart_zhou)
|
|
|
|
|
|
|
|
# 使用set修改关联数据
|
|
|
|
user_design = UserExtendInfo.objects.filter(department='设计部').all()
|
|
|
|
skill_ps = SkillInfo.objects.get(skill_name='Photoshop')
|
|
|
|
skill_ps.user.set(user_design)
|
|
|
|
|
|
|
|
# 直接给定id
|
|
|
|
skill_draw = SkillInfo.objects.get(skill_name='draw')
|
|
|
|
skill_draw.user.set([3])
|
|
|
|
|
|
|
|
# 批量关联多对多数据
|
|
|
|
skill_dev = SkillInfo.objects.filter(skill_name__in=['JavaScript', 'PHP']).all()
|
|
|
|
user_dev = UserExtendInfo.objects.filter(department='开发部').all()
|
|
|
|
for skill in skill_dev:
|
|
|
|
skill.user.add(*user_dev)
|
|
|
|
|
|
|
|
# 更新数据
|
|
|
|
# 查询出结果后通过赋值方式使用save方法保存
|
|
|
|
skill_data = SkillInfo.objects.filter(skill_name='测试').first()
|
|
|
|
skill_data.skill_name = 'Test'
|
|
|
|
skill_data.save()
|
|
|
|
|
|
|
|
# 使用update方式直接修改
|
|
|
|
skill_data1 = SkillInfo.objects.filter(skill_name='画图').update(skill_name='draw')
|
|
|
|
|
|
|
|
# 删除数据 返回删除结果 (2, {'orm_demo.SkillInfo': 2})
|
|
|
|
SkillInfo.objects.create(skill_name='delete')
|
|
|
|
SkillInfo.objects.create(skill_name='delete')
|
|
|
|
del_skill = SkillInfo.objects.filter(skill_name='delete').delete()
|
|
|
|
print(del_skill)
|
|
|
|
|
|
|
|
# 关联表查询 一对一
|
|
|
|
# 通过扩展表名查询
|
|
|
|
user_li = BaseUserInfo.objects.filter(username='李四').first()
|
|
|
|
print(user_li.userextendinfo.true_name) # 扩展表名需要小写, 返回:李四喜
|
|
|
|
|
|
|
|
# 通过关联字段查询
|
|
|
|
user_extend_li = UserExtendInfo.objects.filter(true_name__contains='李四').first()
|
|
|
|
print(user_extend_li.user.password) # 使用关联字段名称, 返回:lisi
|
|
|
|
|
|
|
|
# 关联表查询 一对多
|
|
|
|
# 正向查询
|
|
|
|
user_zhang = BaseUserInfo.objects.get(id=1)
|
|
|
|
card_zhang = user_zhang.cardinfo_set.all() # 使用小写的表名加上_set做为关联字段
|
|
|
|
for card in card_zhang:
|
|
|
|
print(card.card_no)
|
|
|
|
|
|
|
|
# 正向查询 related_name 指定关联名称
|
|
|
|
user_zhang = BaseUserInfo.objects.get(id=1)
|
|
|
|
card_zhang = user_zhang.user_card.all() # 使用指定的关联名做为关联字段
|
|
|
|
for card in card_zhang:
|
|
|
|
print(card.card_no)
|
|
|
|
|
|
|
|
# 反向查询
|
|
|
|
card = CardInfo.objects.filter(user_id=1).first()
|
|
|
|
print(card.user.username)
|
|
|
|
|
|
|
|
# 多对多查询
|
|
|
|
# 正向查询
|
|
|
|
user_zhang = UserExtendInfo.objects.get(username='张三')
|
|
|
|
skill_user_zhang = user_zhang.skillinfo_set.all()
|
|
|
|
print(skill_user_zhang)
|
|
|
|
|
|
|
|
# 反向查询
|
|
|
|
skill_py = SkillInfo.objects.filter(skill_name='Python').first()
|
|
|
|
for user in skill_py.user.all():
|
|
|
|
print(user.true_name)
|
|
|
|
|
|
|
|
# 多对多删除关联数据 remove clear
|
|
|
|
# 删除指定用户的指定技能
|
|
|
|
user_del = UserExtendInfo.objects.get(id=5)
|
|
|
|
skill_del = SkillInfo.objects.get(id=7)
|
|
|
|
result = skill_del.user.remove(user_del)
|
|
|
|
|
|
|
|
# 通过id指定删除哪个用户
|
|
|
|
skill_del = SkillInfo.objects.get(id=1)
|
|
|
|
result = skill_del.user.remove(2)
|
|
|
|
|
|
|
|
# 清除全部
|
|
|
|
skill_del = SkillInfo.objects.get(id=8)
|
|
|
|
result = skill_del.user.clear()
|