python_django数据关系一对多、多对多模型、自关联的建立

2022-10-0309:08:35后端程序开发Comments766 views字数 2891阅读模式

一对多模型

一对多的关系,例如员工跟部门。一个部门有多个员工。那么在django怎么建立这种表关系呢?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

其实就是利用外键,在多的一方,字段指定外键即可。例如员工和部门,员工是多,所以在员工表直接部门即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

示例(见19行):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Department(models.Model):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

name = models.CharField(max_length=20)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

create_data = models.DateField(auto_now_add=True)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

is_delete = models.BooleanField(default=False)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Meta:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

db_table = "department"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Employee(models.Model):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

name = models.CharField(max_length=20)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

age = models.IntegerField()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

gender = models.IntegerField(default=0)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

# decimal_place = 2表示两位小数,max_digits表示8个数字,包括小数的两位文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

salary = models.DecimalField(max_digits=8,decimal_places=2)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

# null=True 表示可以为空,blank=True表示django后台管理输入这个字段可以为空文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

comment = models.CharField(max_length=300,null=True,blank=True)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

hire_data = models.DateField(auto_now_add=True)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

department = models.ForeignKey("Department")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Meta:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

db_table = "employee"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

拓展:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

1.在设置外键时,需要通过on_delete选项指明主表删除数据时,对于外键引用表数据如何处理,在django.db.models中包含了可选常量:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

关联属性on_delete选项的取值文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

  • models.CASCADE 此为默认值,级联删除,会删除关联数据
  • department = models.ForeignKey('Department', on_delete=models.CASCADE)
  • models.PROTECT 只要存在关联数据就不能删除
  • department = models.ForeignKey('Department', on_delete=models.PROTECT)
  • models.SET_NULL 删除数据后关联字段设置为NULL,仅在该字段允许为null时可用(null=True)

2.如果关联的字段不在该应用文件夹的model.py中,那么要写成这样文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

department = models.ForeignKey("(应用文件夹名).Department")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

还有一个需要特别注意:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

department = models.ForeignKey("Department",related_name='employee')时,通过部门查找员工的是用employee。如果不设置的话,是用默认的employee_set(类名的小写+_set)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

一对多的查询:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

一个员工所属的部门(查出来的是对象):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

a = Employee.objects.get(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

b = a.department文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

一个部门的全部员工(查出来的是对象):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

a = Department.objects.get(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

b = a.employee_set.all()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

多对多模型

多对多的关系,例如学生与社团。一个学生可以进多个社团,一个社团可以有多个学生。那么在django怎么建立这种表关系呢?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

django建立多对多关系有两种方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

方法一:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Student(models.Model):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

name= models.CharField(max_length=16)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

birthday=models.DateField()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Club(models.Model):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

name= models.CharField(max_length=16)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

members = models.ManyToManyField("Student")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

只需要在任意一方加上类似第6行的ManyToManyField就可以了。Django会自动为多对多关联关系创建一张表,用于两张表的联系。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

那么查询呢?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

1.一个社团的全部成员(查出来的是对象)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

c = Club.objects.get(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

c.members.all()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

2.一个成员的全部社团(查出来的是对象)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

s = Student.objects.filter(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

s.club_set.all() # 类名的小写+_set文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

方法二:(比较灵活)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

自己手动建立一张表关联联系。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Student(models.Model):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

name= models.CharField(max_length=16)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

birthday=models.DateField()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Club(models.Model):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

name= models.CharField(max_length=16)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Membership(models.Model):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

student = models.ForeignKey("Student")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

club = models.ForeignKey("Club")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

那么这种方式建表怎么查询呢?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

一个学生加入的全部社团:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

a = Student.objects.get(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

b = a.membership_set.all() # 查出来的是对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

for i in b:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

print(i.club.name)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

一个社团的全部学生:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

a = Club.objects.get(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

b = a.membership_set.all() # 查出来的是对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

for i in b:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

print(i.student.name)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

自关联模型

自关联模型,就是表中的某一列,关联了这个表中的另外一列。最典型的自关联模型就是地区表。省、市、县都在一张表里面。省的pid为null,市的pid为省的id,县的pid为市的id。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

示例:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Area(models.Model):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

name = models.CharField(max_length=20, verbose_name='名称')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

# 自关联(特殊的一对多): 生成的字段名 parent_id文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

parent = models.ForeignKey('self', verbose_name='上级行政区划')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

class Meta:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

db_table = 'tb_areas'文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

verbose_name = '行政区划'文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

那么,怎么查询呢?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

如果知道一个市,叫a市,想查他属于什么省。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

a = Area.objects.get(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

# b就是a市的省份的对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

b = a.parent文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

如果知道一个省,叫a省,想查他有什么市。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

a = Area.object.get(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

# b就是a省的全部市的对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

b = a.area_set.all() #类名小写+'_set'文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28171.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/28171.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定