Django表关系的实现

Posted remoting-py

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django表关系的实现相关的知识,希望对你有一定的参考价值。

 

1.首先创建以下5张表模型,表之间的以应关系如下:

技术分享图片

2.模型类:

#学院表
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)
    city = models.CharField(max_length=30,null=True)

    def __str__(self):
        return Stu_detail<s_id=%s,age=%s,gender=%s,city=%s>               %(self.s_id,self.age,self.gender,self.city)

3.创建好后,使用manage.py管理工具把模型映射到数据库中,需要注意的是在Django ORM中多对多关系的中间表会自动创建.

4.查看数据库:

 

5.添加有查询数据:

def add_info(request):
    #一对多关系添加数据
    # d1 = Department()
    # d1.d_name = ‘软件‘
    # d1.save()
    # s1 = Student()
    # s1.s_name = ‘蓝靓钦‘
    # s1.department = d1
    # s1.save()
    #多对多关系添加数据
    c1 = Course()
    c1.c_name = django
    c1.save() #这是往课程信息表中添加数据
    #查找学生信息表中name包含‘蓝靓钦‘的学生
    s1 = Student.objects.filter(s_name__contains=蓝靓钦)[0]
    s1.course.add(c1)
    return HttpResponse(插入数据)

def search_info(request):
    rs = Student.objects.filter(s_name__contains=蓝靓钦)[0]
    #一对多关系查询
    print(rs.department)
    #多对多关系查询,必有用all方法
    print(rs.course.all())
    #反向查找,通过课程查找学生
    cs = Course.objects.first()
    print(cs.student_set.all())
    return HttpResponse(查找)

 

mysql> select * from info_department;
+------+--------+
| d_id | d_name |
+------+--------+
|    1 | 软件   |
+------+--------+
1 row in set (0.00 sec)

mysql> select * from info_student;
+------+-----------+---------------+
| s_id | s_name    | department_id |
+------+-----------+---------------+
|    1 | 蓝靓钦    |             1 |
+------+-----------+---------------+
1 row in set (0.00 sec)

mysql> select * from info_course;
+------+--------+
| c_id | c_name |
+------+--------+
|    1 | python |
+------+--------+
1 row in set (0.00 sec)

mysql> select * from info_student_course;
+----+------------+-----------+
| id | student_id | course_id |
+----+------------+-----------+
|  1 |          1 |         1 |
+----+------------+-----------+
1 row in set (0.00 sec)

mysql> select * from info_student_course;
+----+------------+-----------+
| id | student_id | course_id |
+----+------------+-----------+
|  1 |          1 |         1 |
|  2 |          1 |         2 |
+----+------------+------

 

技术分享图片

 

以上是关于Django表关系的实现的主要内容,如果未能解决你的问题,请参考以下文章

26.Django实现表关系(一对多,一对一,多对多))

26.Django实现表关系(一对多,一对一,多对多))

django ORM中的表关系

Django 第十课 3.ORM表关系

django的多表操作

Django