亲宝软件园·资讯

展开

Django ORM多对多查询方法(自定义第三张表&ManyToManyField)

人气:0

对于多对多表

- 1.自定义第三张表,更加灵活

- 2.ManyToManyField 自动生成第3张表 只能 有3列数据 不能自己添加。

自定义第三张表

# 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')

  # 创建联合唯一索引
  # 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小于有关系的boy

# 1 通过girl表进行反向查找
  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表进行查找

  obj = models.Love.objects.filter(g__name='小于').all()
  for i in obj:
    print(i.b.name)

  # 这样进行查询性能不好,需要重新发sql请求在进行查询另外一张表中的数据

  boy_list = models.Love.objects.filter(g__name='小于').values('b__name')
  for i in boy_list:
    print(i['b__name'])

  # 这样 进行了 优化 不会重复发送sql请求,查询到的结果内是字典

  boy_obj = models.Love.objects.filter(g__name='小于').select_related('b').all()
  for i in boy_obj:
    print(i.b.name)
  # select_related('ut') 相当 于 inner join 先连成一张表再进行查询

  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张表,如果不加后面产生生成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() # 可以

  # 杂交的方法对获取查询方便点,还添加了清空功能

  boy_obj = obj.m.all()

  for i in boy_obj:
    print(i.name)

以上这篇Django ORM多对多查询方法(自定义第三张表&ManyToManyField)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论