django模型
知识要点:表关系的实现
一对多表关系
在Mysql中一对多是通过外键实现的,在django模型中通过ForeignKeyfield
类型实现。
一对一表关系
在Mysql中一对一是通过外键加唯一键实现的,在django模型中通过OneToOneField
类型实现。
多对多表关系
在Mysql中多对多是通过中间表外键加联合唯一键实现的,在django模型中通过ManyToManyField
类型实现。中间表模型会自动帮我们创建好。
例子:
#终于理解了,按照sql语句的思想去建表,来对应django的模型类映射
#django是按照sql建表的正向思路去建表,不是反向的去建模型类去映射
#也就是说按照逻辑关系去建表
#学院和学生(一对多)
#比如先建一张学院表
#再建一张学生表,那么我们在建学生表的时候考虑,
#学生属于哪个学院,这是添加department字段
#学生和学生信息(一对一)
#再比如现在建一张学生信息表
#那么一条记录对应的是哪个学生呢?
#此时,加上s_id字段,代表从学生表里面选,对应的学生id
#学生和课程(多对多)
#至于现在我们建一张课程表
#哪些学生选了哪些课程呢?
#注意是哪些对哪些,也就是多对多
#两张表已经无法满足,所以这里需要创建中间表,
#也就是关系表
class Department(models.Model):
d_id = models.AutoField(primary_key=True)
d_name = models.CharField(max_length=30)
def __str__(self):
return 'Department<d_id=%s,d_name=%s>'%(
self.d_id,self.d_name
)
class Student(models.Model):
s_id = models.AutoField(primary_key=True)
s_name = models.CharField(max_length=30)
department = models.ForeignKey('Department')
course = models.ManyToManyField('Course')
def __str__(self):
return 'Student<s_id=%s,s_name=%s>'%(
self.s_id,self.s_name
)
class Course(models.Model):
c_id = models.AutoField(primary_key=True)
c_name = models.CharField(max_length=30)
def __str__(self):
return 'Course<c_id=%s,c_name=%s>'%(
self.c_id,self.c_name
)
class Stu_detail(models.Model):
s_id = models.OneToOneField('Student')
age = models.IntegerField()
gender = models.BooleanField(default=1)
country = models.CharField(max_length=30,null=True)
def __str__(self):
return 'Stu_detail<s_id=%s,age=%s,gender=%s,country=%s>'%(
self.s_id,self.age,self.gender,self.country
)
#映射到mysql中后
#学院表
mysql> show columns from `music_department`;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| d_id | int(11) | NO | PRI | NULL | auto_increment |
| d_name | varchar(60) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
#学生表 学院对学生 (一对多)
#department = models.ForeignKey('Department')
#映射到mysql中属性department,自动追加_id变成字段department_id
注意course = models.ManyToManyField('Course')
这个属性没有在mysql中映射字段
mysql> show columns from `music_student`;
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| s_id | int(11) | NO | PRI | NULL | auto_increment |
| s_name | varchar(60) | NO | | NULL | |
| department_id | int(11) | NO | MUL | NULL | |
+---------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
#课程表
mysql> show columns from `music_course`;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| c_id | int(11) | NO | PRI | NULL | auto_increment |
| c_name | varchar(30) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
#学生和详情 (一对一)
#s_id = models.OneToOneField('Student')
#映射到mysql自动给属性添加了s_id_id
mysql> show columns from `music_stu_detail`;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| age | int(11) | NO | | NULL | |
| gender | tinyint(1) | NO | | NULL | |
| city | varchar(30) | YES | | NULL | |
| s_id_id | int(11) | NO | UNI | NULL | |
+---------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
#中间表 (多对多关系)自动生成
#course = models.ManyToManyField('Course')
#自动创建了中间表`music_student_course`
mysql> show columns from `music_student_course`;
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| student_id | int(11) | NO | MUL | NULL | |
| course_id | int(11) | NO | MUL | NULL | |
+------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
关系表中数据的操作
from .models import Department,Student,Course
def add_info(request):
# d1 = Department(d_name='CC')
# d1.save()
# 一对多关系加内容
# s1 = Student(s_name='xiaoming')
# s1.department = d1
# s1.save()
# 多对多关系添加内容
# c1 = Course(c_name='python')
# s1 = Student.objects.first()
# c1.save()
# s1.course.add(c1)
return HttpResponse('添加数据成功')
def search_info(request):
rs = Student.objects.all()[0]
# 一对多的查询
print(rs.department)
# 多对多的正向查询
print(rs.course.all())
cs = Course.objects.first()
# 多对多反向查询
print(cs.student_set.all())
return HttpResponse('查询数据成功')