From 14b08604490cdbdbaa8b747279aa9785a17091bb Mon Sep 17 00:00:00 2001 From: roger_home_pc Date: Sat, 8 Jul 2023 22:31:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=A2=9E=E5=88=A0=E6=94=B9?= =?UTF-8?q?=E7=9A=84=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- orm_demo/migrations/0002_cardinfo.py | 23 ++++++ ...info_table_comment_alter_cardinfo_table.py | 21 ++++++ .../migrations/0004_alter_cardinfo_user.py | 19 +++++ orm_demo/models.py | 11 +++ orm_demo/orm_crud.py | 68 +++++++++++++++++- requirement.txt | Bin 0 -> 102 bytes 6 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 orm_demo/migrations/0002_cardinfo.py create mode 100644 orm_demo/migrations/0003_alter_cardinfo_table_comment_alter_cardinfo_table.py create mode 100644 orm_demo/migrations/0004_alter_cardinfo_user.py create mode 100644 requirement.txt diff --git a/orm_demo/migrations/0002_cardinfo.py b/orm_demo/migrations/0002_cardinfo.py new file mode 100644 index 0000000..6057e06 --- /dev/null +++ b/orm_demo/migrations/0002_cardinfo.py @@ -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')), + ], + ), + ] diff --git a/orm_demo/migrations/0003_alter_cardinfo_table_comment_alter_cardinfo_table.py b/orm_demo/migrations/0003_alter_cardinfo_table_comment_alter_cardinfo_table.py new file mode 100644 index 0000000..0c1e395 --- /dev/null +++ b/orm_demo/migrations/0003_alter_cardinfo_table_comment_alter_cardinfo_table.py @@ -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', + ), + ] diff --git a/orm_demo/migrations/0004_alter_cardinfo_user.py b/orm_demo/migrations/0004_alter_cardinfo_user.py new file mode 100644 index 0000000..7ab119d --- /dev/null +++ b/orm_demo/migrations/0004_alter_cardinfo_user.py @@ -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'), + ), + ] diff --git a/orm_demo/models.py b/orm_demo/models.py index f327f2e..2a47bad 100644 --- a/orm_demo/models.py +++ b/orm_demo/models.py @@ -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 = '员工银行卡信息' diff --git a/orm_demo/orm_crud.py b/orm_demo/orm_crud.py index f0d1ccb..a9fa6da 100644 --- a/orm_demo/orm_crud.py +++ b/orm_demo/orm_crud.py @@ -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) diff --git a/requirement.txt b/requirement.txt new file mode 100644 index 0000000000000000000000000000000000000000..51e258236dd71fc4ecfb718515d859891973ce18 GIT binary patch literal 102 zcmW-ZJq|!X7zL-6xQeWwLZfpL5n+WzY~%8H`{iY-nY{1qSs4h-9CTVkK_zN(GCU^_ ckwrF`Z{

oE5wL&BewjJd?U;WqL_Ce!$@ny8r+H literal 0 HcmV?d00001