Django中manytomanyField自定义第三张表

2021/10/16 23:11:19

本文主要是介绍Django中manytomanyField自定义第三张表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

自定义第三张表:

# 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中manytomanyField自定义第三张表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程