完成增删改的学习

master
roger_home_pc 1 year ago
parent 823f340fc5
commit 14b0860449
  1. 23
      orm_demo/migrations/0002_cardinfo.py
  2. 21
      orm_demo/migrations/0003_alter_cardinfo_table_comment_alter_cardinfo_table.py
  3. 19
      orm_demo/migrations/0004_alter_cardinfo_user.py
  4. 11
      orm_demo/models.py
  5. 68
      orm_demo/orm_crud.py
  6. BIN
      requirement.txt

@ -0,0 +1,23 @@
# Generated by Django 4.2.3 on 2023-07-08 02:10
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('orm_demo', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='CardInfo',
fields=[
('id', models.AutoField(primary_key=True, serialize=False, verbose_name='银行卡编码')),
('card_no', models.CharField(max_length=20, verbose_name='卡号')),
('bank', models.CharField(max_length=10, verbose_name='银行')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='orm_demo.baseuserinfo')),
],
),
]

@ -0,0 +1,21 @@
# Generated by Django 4.2.3 on 2023-07-08 02:12
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('orm_demo', '0002_cardinfo'),
]
operations = [
migrations.AlterModelTableComment(
name='cardinfo',
table_comment='员工银行卡信息',
),
migrations.AlterModelTable(
name='cardinfo',
table='orm_card_info',
),
]

@ -0,0 +1,19 @@
# Generated by Django 4.2.3 on 2023-07-08 03:44
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('orm_demo', '0003_alter_cardinfo_table_comment_alter_cardinfo_table'),
]
operations = [
migrations.AlterField(
model_name='cardinfo',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user_card', to='orm_demo.baseuserinfo'),
),
]

@ -63,3 +63,14 @@ class SkillInfo(models.Model):
class Meta:
db_table = 'orm_skill_info'
db_table_comment = '技能信息'
class CardInfo(models.Model):
id = models.AutoField(verbose_name='银行卡编码', primary_key=True)
card_no = models.CharField(verbose_name='卡号', max_length=20)
bank = models.CharField(verbose_name='银行', max_length=10)
user = models.ForeignKey(BaseUserInfo, related_name='user_card', on_delete=models.CASCADE)
class Meta:
db_table = 'orm_card_info'
db_table_comment = '员工银行卡信息'

@ -15,7 +15,8 @@ user = BaseUserInfo.objects.create(username='张三', password='zhangsan', statu
# create方式赋值, 字典格式数据
data = {'username': '李四', 'password': 'lisi', 'status': 1}
user = BaseUserInfo.objects.create(**data)
# data2 = dict(username='李四', password='lisi', status=1) # 另一种字典数据定义形势
user2 = BaseUserInfo.objects.create(**data)
# 批量插入
data_list = [
@ -34,3 +35,68 @@ 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字段数据
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)
# 批量关联多对多数据
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)

Binary file not shown.
Loading…
Cancel
Save