Python自动化之一对多
2024-08-24 20:40:32
一对多
建立一对多关系之后(也就是加了外键),会在字表里面多一个“外键字段_id”这个字段
查询
#_*_coding:utf-8_*_
from django.db import models
# Create your models here.
class Colors(models.Model):
colors=models.CharField(max_length=10) #蓝色
def __str__(self):
return self.colors
class Ball(models.Model):
color=models.OneToOneField("Colors") #与颜色表为一对一,颜色表为母表
description=models.CharField(max_length=10) #描述
def __str__(self):
return self.description
class Clothes(models.Model):
color=models.ForeignKey("Colors") #与颜色表为外键,颜色表为母表
description=models.CharField(max_length=10) #描述
def __str__(self):
return self.description
class Child(models.Model):
name=models.CharField(max_length=10) #姓名
favor=models.ManyToManyField('Colors') #与颜色表为多对多
外键表联合查询:
#外键子表查询母表,与一对一子表查询母表形式一致
#找到红裤衩所属的颜色表中的颜色--返回:红
#写法1:
通过子表查询母表,写法:"子表对象.母表表名的小写.母表字段名" ;通过Clothes表查到description为"小虎哥",查找到对应colors
print(models.Clothes.objects.get(description="小虎哥").color.colors) #返回红,通过子表查询母表,写法:"子表对象.母表表名的小写.母表字段名" ;通过Clothes表查到description为"小虎哥",查找到对应colors
#写法2,反向从母表入手:
通过子表查询母表,但形式上是从母表对象自身直接获取字段,写法:"母表.objects.get(子表名小写__子表字段="xxx").母表字段名" ;
print(models.Colors.objects.get(clothes__description="小虎哥").colors) #返回红,通过子表查询母表,但形式上是从母表对象自身直接获取字段,写法:"母表.objects.get(子表名小写__子表字段="xxx").母表字段名" ;效果和上边完全一致,另一种形式
#外键母表查询子表,与一对一形式不同,因为母表为"多",不能像一对一一样通过.get().子表.子表字段的方式获取,但与多对多母表查询子表一致
#找到颜色为红的所有服装--返回:[<Clothes: 大美女>, <Clothes: 小虎哥>]
#写法1:
color_obj=models.Colors.objects.get(colors="红")
print(color_obj.clothes_set.all()) #注意:子表小写_set的写法,它实际上是一个QuerySet,可以用update,delete,all,filter等方法
#写法2:
print(models.Clothes.objects.filter(color=models.Colors.objects.get(colors="红")))
#写法2简便写法(推荐):
print(models.Clothes.objects.filter(color__colors="红")) #写法:filter(子表外键字段__母表字段='过滤条件')
#写法3:
color_id=models.Colors.objects.get(colors="红").id #通过母表获取到颜色为红的id
print(models.Clothes.objects.filter(color_id=color_id)) #filter得到QuerySet,写法:filter(子表外键字段_母表主键=母表主键对象)
增
#增添子表数据,形式与一对一一致
#添加颜色为绿的服装:小帅哥
#方法1:
models.Clothes.objects.create(color=models.Colors.objects.get(colors="绿"),description="小帅哥")
#方法1补充:
models.Clothes.objects.create(color_id=models.Colors.objects.get(colors="绿").id,description="小帅哥")
#方法2:
c_obj=models.Clothes(color=models.Colors.objects.get(colors="绿"),description="小帅哥")
c_obj.save()
#方法3:字典方式录入..参考一对一
改
#颜色为红的服装,description都更新为大美女
#写法1:
models.Clothes.objects.filter(color__colors="红").update(description="大美女")
#写法2:
models.Clothes.objects.filter(color_id=models.Colors.objects.get(colors="红").id).update(description="大美女")
#写法3:
colors_obj=models.Colors.objects.get(colors="红")
colors_obj.clothes_set.filter(id__gte=1).update(description="大美女")
#其他写法参照一对一的修改和外键的查询
删
models.Clothes.objects.get(description="灰裙子").delete() #对象和QuerySet都有方法delete()
models.Colors.objects.filter(colors="灰").delete()
一对多示例2
class UserGroup(models.Model):
uid = models.AutoField(primary_key=True)
caption = models.CharField(max_length=32,unique=True)
ctime = models.DateTimeField(auto_now_add=True, null=True)
uptime = models.DateTimeField(auto_now=True, null=True)
# f = models.ForeignKey('Foo')
#
# user_list = Userinfo.objects.all()
# for row in user_list:
# print(row.user_group_id)
# print(row.user_group.uid)
# print(row.user_group.caption)
class UserInfo(models.Model):
# id列,自增,主键
# 用户名列,字符串类型,指定长度
# 字符串、数字、时间、二进制
username = models.CharField(max_length=32,blank=True,verbose_name='用户名')
password = models.CharField(max_length=60, help_text='pwd')
email = models.CharField(max_length=60)
test = models.EmailField(max_length=19,null=True,error_messages={'invalid': '请输入密码'})
# user_group_id 数字
user_group = models.ForeignKey("UserGroup",to_field='uid') # (uid,catption,ctime,uptimew)
user_type_choices = (
(1, '超级用户'),
(2, '普通用户'),
(3, '普普通用户'),
)
user_type_id = models.IntegerField(choices=user_type_choices,default=1)
最新文章
- hihoCoder 1196 高斯消元&#183;二
- Magento Connector: Error: Please check for sufficient write file permissions
- HttpServletRequest常用的方法
- DB2导入导出编目配置
- spring之BeanFactoryAware接口
- weiphp执行的流程
- JQuery基础二
- android setVisibility失效不起作用的问题
- MSSQLSERVER之发布-分发-订阅
- 2463: [中山市选2009]谁能赢呢?- BZOJ
- Could not bind factory to JNDI
- Solr Update备注
- ViewState 和字段属性的差异
- oracle数据库连接缓慢
- Linux列字符替换
- 032、学容器必须懂bridge网络(2019-02-19 周二)
- Linux系统根据端口号来查看其进程并杀死进程
- C语言程序设计II—第五周教学
- Python+Selenium学习--访问连接
- BeautifulSoup 使用select方法详解(通过标签名,类名, id,组合,属性查找)
热门文章
- Java8并发教程:Threads和Executors
- 面试中关于Java你所需知道的的一切
- jQuery的一些常用的方法(转载)
- Yeelink 初探
- [转]Python yield 使用浅析
- phpstorm 激活服务器
- javascript语法速查表
- 剑指offer 面试题64 数据流的中位数
- 如何解决wow.js与fullpage的兼容性
- Mongo运行错误:Failed to connect 127.0.0.1:27017,reason:errno:10061由于目标计算机积极拒绝,无法连接