對于多對多表
1.自定義第三張表蜂怎,更加靈活
2.ManyToManyField 自動生成第3張表 只能 有3列數(shù)據(jù) 不能自己添加菇存。
自定義第三張表
# models.py
class Boy(models.Model):
name = models.CharField(max_length=32)
class Girl(models.Model):
name = models.CharField(max_length=32)
class Love(models.Model):
b = models.ForeignKey('Boy')
g = models.ForeignKey('Girl')
# 創(chuàng)建聯(lián)合唯一索引
# class Meta:
# unique_together = [
# ('b','g'),
# ]
# views.py
def test(request):
# objs = [
# models.Boy(name='summer'),
# models.Boy(name='spring'),
# models.Boy(name='autumn'),
# ]
# models.Boy.objects.bulk_create(objs,5)
# objs = [
# models.Girl(name='小于'),
# models.Girl(name='小秋'),
# models.Girl(name='小夏'),
# ]
# models.Girl.objects.bulk_create(objs,5)
# 查詢和girl小于有關(guān)系的boy
# 1 通過girl表進(jìn)行反向查找
obj = models.Girl.objects.filter(name='小于').first().love_set.all()
# obj = models.Girl.objects.filter(name='小于').first().love_set.select_related('b').all()
for i in obj:
print(i.b.name)
print(' '.center(60,'='))
# 2 通過Love表進(jìn)行查找
obj = models.Love.objects.filter(g__name='小于').all()
for i in obj:
print(i.b.name)
# 這樣進(jìn)行查詢性能不好,需要重新發(fā)sql請求在進(jìn)行查詢另外一張表中的數(shù)據(jù)
boy_list = models.Love.objects.filter(g__name='小于').values('b__name')
for i in boy_list:
print(i['b__name'])
# 這樣 進(jìn)行了 優(yōu)化 不會重復(fù)發(fā)送sql請求君编,查詢到的結(jié)果內(nèi)是字典
boy_obj = models.Love.objects.filter(g__name='小于').select_related('b').all()
for i in boy_obj:
print(i.b.name)
# select_related('ut') 相當(dāng) 于 inner join 先連成一張表再進(jìn)行查詢
return HttpResponse('insert ok ....')
- ManyToManyField
# models.py
from django.db import models
class Boy(models.Model):
name = models.CharField(max_length=32)
# m = models.ManyToManyField('Girl')
class Girl(models.Model):
name = models.CharField(max_length=32)
m = models.ManyToManyField('Boy')
# views.py
def test(request):
# objs = [
# models.Boy(name='summer'),
# models.Boy(name='spring'),
# models.Boy(name='autumn'),
# ]
# models.Boy.objects.bulk_create(objs,5)
# objs = [
# models.Girl(name='小于'),
# models.Girl(name='小秋'),
# models.Girl(name='小夏'),
# ]
# models.Girl.objects.bulk_create(objs,5)
# ManyToManyField
obj = models.Girl.objects.filter(name='小秋').first()
# 增
# obj.m.add(3)
# obj.m.add(3,4)
# obj.m.add(*[1,2])
# 刪
# obj.m.remove(3)
# obj.m.remove(3,4)
# obj.m.remove(*[1,2])
# 改
# obj.m.set([1,2,])
# boy_obj = obj.m.all()
# for row in boy_obj:
# print(row.id,row.name)
# 清除
# obj.m.clear()
# 反向查 在沒有 ManyToManyField 字段的表中查
obj = models.Boy.objects.filter(name='summer').first()
girl_obj = obj.girl_set.all()
# 增刪改查
obj.girl_set.set([1,2,3])
for row in girl_list:
print(row.id,row.name)
return HttpResponse('ok')
- 雜交(自定義第三張表+ManyToManyField)
# modles.py
class Boy(models.Model):
name = models.CharField(max_length=32)
class Girl(models.Model):
name = models.CharField(max_length=32)
m = models.ManyToManyField('Boy',through='Love',through_fields=('b','g')) # 只讓其生成3張表,如果不加后面產(chǎn)生生成4張表
class Love(models.Model):
b = models.ForeignKey('Boy')
g = models.ForeignKey('Girl')
# views.py
def test(request):
obj = models.Girl.objects.filter(name='小于').first()
# obj.m.add(1) # 不行
# obj.m.remove(1) # 不行
# obj.m.set([1,2]) # 不行
# obj.m.clear() # 可以
# obj.m.all() # 可以
# 雜交的方法對獲取查詢方便點(diǎn),還添加了清空功能
boy_obj = obj.m.all()
for i in boy_obj:
print(i.name)