$Django 多对多-自定义第三张表 基于双下划线的跨表查询(补充)
自定义第三张表的好处:可以定义多个字段,
缺点:查询不方便(有方法解决)
1.第三张表设置外键,联合唯一(查询不方便)
class Books(models.Model):
name=models.CharField(max_length=32)
price=models.DecimalField(max_digits=6,decimal_places=2)
class Zuozhes(models.Model):
name=models.CharField(max_length=32)
sex=models.BooleanField()
# 第三张表
class b_z(models.Model):
book=models.ForeignKey(to='Books',to_field='id')
zuozhe=models.ForeignKey(to='Zuozhes',to_field='id')
class Meta:
unique_together=('book','zuozhe')
import os
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dj23.settings")
import django
django.setup() from app01.models import *
# 添加书,作者,关联
a1=Books.objects.create(name='西游记2',price=30)
print(a1) #object
b1=Zuozhes.objects.create(name='lqz2',sex=True)
b2=Zuozhes.objects.create(name='egon2',sex=True)
b_z.objects.create(book=a1,zuozhe=b1)
b_z.objects.create(book=a1,zuozhe=b2)
# 跨表查询
# 查询书名为西游记的 作者名
b=Books.objects.filter(name='西游记')
b=[ i for i in b]
z=b_z.objects.filter(book__in=b)
z=[i.zuozhe_id for i in z]
g=Zuozhes.objects.filter(id__in=z).values('name')
print(g) #<QuerySet [{'name': 'lqz'}, {'name': 'egon'}]>
# 查询查询书名为西游记2的 作者名
u=Books.objects.filter(name='西游记2')
u=[i for i in u]
c=b_z.objects.filter(book__in=u).values('zuozhe__name')
print(c) #<QuerySet [{'zuozhe__name': 'lqz2'}, {'zuozhe__name':}]> 2.第三张表设置外键,联合唯一(查询方便)
class Books(models.Model):
name=models.CharField(max_length=32)
price=models.DecimalField(max_digits=6,decimal_places=2)
zuo=models.ManyToManyField(to='Zuozhes',through='b_z',through_fields=('book','zuozhe'))
# 关联字段就是表名小写, 第一个值: 就是当前表的表名小写,顺序很重要
class Zuozhes(models.Model):
name=models.CharField(max_length=32)
sex=models.BooleanField()
# 第三张表
class b_z(models.Model):
book=models.ForeignKey(to='Books',to_field='id')
zuozhe=models.ForeignKey(to='Zuozhes',to_field='id')
class Meta:
unique_together=('book','zuozhe')
from app02 import models as m
# 添加书,作者,关联
n1=m.Books.objects.create(name='校花贴身高手',price=66)
n2=m.Books.objects.create(name='蛮荒',price=60)
v1=m.Zuozhes.objects.create(name='零一',sex=True)
v2=m.Zuozhes.objects.create(name='娥',sex=True)
v3=m.Zuozhes.objects.create(name='牧尘',sex=True)
v4=m.Zuozhes.objects.create(name='额',sex=True)
m.b_z.objects.create(book=n1,zuozhe=v1)
m.b_z.objects.create(book=n1,zuozhe=v2)
m.b_z.objects.create(book=n2,zuozhe=v3)
m.b_z.objects.create(book=n2,zuozhe=v4)
# 查询查询书名为校花贴身高手的作者名
b1=m.Books.objects.filter(name='校花贴身高手').values('zuo__name')
print(b1) #<QuerySet [{'zuo__name': '零一'}, {'zuo__name': '娥'}]>
b2=m.Books.objects.filter (name='校花贴身高手')
b2=[i.zuo for i in b2]
for i in b2:
print(i) #app02.Zuozhes.None
for z in i.all():
print(z.name) #零一 娥
# 查询查询书名为蛮荒的作者名
p=m.Zuozhes.objects.filter(books__name='蛮荒').values('name')
print(p) #<QuerySet [{'name': '牧尘'}, {'name': '额'}]>
基于双下划线跨表查询精髓:正向:book表查作者 内有zuozhe外键字段(按字段 查) Book.object.filter(zuozhe__name='xxx')
反向:zuozhe表查书 (按表名小写 查) Zuozhe.object.filter(book__name__in=['ooo','yyy'])
随机推荐
- Part-One
首先,必须要声明一下,这个目录下的所有东西,是我对一本书复习,只是敲出部分代码让自己不至于眼高手低,其中有很多东西可能都是我的个人理解,如果有兴趣的朋友可以看一下,同时也欢迎大家指正. 1.Hello ...
- IO流--与properties集合配合使用
IO流--与properties集合配合使用: 注:生产上主要用于常量文件的配置,读取常量文件: 1:properties集合的放值与取值: /* * properties集合继承自hashTable ...
- 使用二分查找判断某个数在某个区间中--如何判断某个IP地址所属的地区
一,问题描述 给定100万个区间对,假设这些区间对是互不重叠的,如何判断某个数属于哪个区间? 首先需要对区间的特性进行分析:区间是不是有序的?有序是指:后一个区间的起始位置要大于前一个区间的终点位置. ...
- T-SQL 编程技巧
Ø T-SQL 编程是大多数程序员都会接触的,也是数据库编程必须掌握的技术.下面,是本人在工作或学习中积累的一些心得和技巧.主要包含以下内容: 1. waitfor延时执行 2. NOT 关 ...
- php下curl ssl常用问题
1. 查看curl版本 在phpinfo中,可以查看 curl cURL support enabled cURL Information 7.35.0 Age 3 Features AsynchDN ...
- 【Ubuntu】安装Java和Eclipse
1. 安装Java 1> sudo add-apt-repository ppa:webupd8team/java 2> sudo apt-get update 3> sudo ap ...
- Debian Security Advisory(Debian安全报告) DSA-4411-1 firefox-esr security update
Debian Security Advisory(Debian安全报告) DSA-4411-1 firefox-esr security update Package :firefox-esr CV ...
- Java SE之正则表达式二:匹配
package demo.regex; import java.util.regex.Pattern; /* 正则表达式:匹配 */ public class RegexMatchesDemo { / ...
- HDU-1018 BigNumber(斯特林近似)
题目链接 斯特林近似求数位长度经典题,更新板子顺手切了 #include <cstdio> #include <cmath> #include <cstring> ...
- nginx 完成缓存清除 以及升级包的自动升级
#!/bin/bash function cache(){ for i in `curl xxx.fe.xxx.cn/uplist.txt`;do url=`echo $i|awk -F"# ...