django 多对多 增 删 改 查

Posted zjltt

tags:

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

 

 

一、通过url方式实现多对多的:增加,删除,编辑

代码目录:

urls.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""django_one URL Configuration
 
The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r\'^$\', views.home, name=\'home\')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r\'^$\', Home.as_view(), name=\'home\')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r\'^blog/\', include(\'blog.urls\'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01.views import classes
from app01.views import students
from app01.views import teachers
 
urlpatterns = [
    url(r\'^admin/\', admin.site.urls),
    url(r\'^get_classes.html$\', classes.get_classes),
    url(r\'^add_classes.html$\', classes.add_classes),
    url(r\'^del_classes.html$\', classes.del_classes),
    url(r\'^edit_classes.html$\', classes.edit_classes),
 
    url(r\'^get_students.html$\', students.get_students),
    url(r\'^add_students.html$\', students.add_students),
    url(r\'^del_students.html$\', students.del_students),
    url(r\'^edit_students.html$\', students.edit_students),
 
    url(r\'^get_teachers.html$\', teachers.get_teachers),
    url(r\'^add_teachers.html$\', teachers.add_teachers),
    url(r\'^del_teachers.html$\', teachers.del_teachers),
    url(r\'^edit_teachers.html$\', teachers.edit_teachers),
 
]

models.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django.db import models
 
# Create your models here.
 
class Classes(models.Model):
    """
    班级表,男
    """
    titile = models.CharField(max_length=32)
    m = models.ManyToManyField("Teachers")
 
class Teachers(models.Model):
    """
    老师表,女
    """
    name = models.CharField (max_length=32)
 
"""
cid_id  tid_id
 1    1
 1    2
 6    1
 1000  1000
"""
# class C2T(models.Model):
#     cid = models.ForeignKey(Classes)
#     tid = models.ForeignKey(Teachers)
 
class Student(models.Model):
    username = models.CharField(max_length=32)
    age = models.IntegerField()
    gender = models.BooleanField()
    cs = models.ForeignKey(Classes)

teachers.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django.shortcuts import render
from django.shortcuts import redirect
from app01 import models
 
 
def get_teachers(request):
    cls_list = models.Classes.objects.all()
    return render(request,\'get_teachers.html\',{\'cls_list\':cls_list})
 
def add_teachers(request):
    if request.method == "GET":
        teachers_list = models.Teachers.objects.all()
        return render(request, \'add_teachers.html\',{\'teachers_list\':teachers_list})
    elif request.method == "POST":
        nid = request.POST.get(\'titile\')
        # [1,2,3,4,]
        teachers_id = request.POST.getlist(\'teachers_id\')
        # 10
        obj = models.Classes.objects.create(titile = nid)
        # 10 1
        # 10 2
        # 10 3
        # 10 4
        obj.m.add(*teachers_id)
        return redirect(\'get_teachers.html\')
 
 
def del_teachers(request):
    nid = request.GET.get(\'nid\')
    models.Classes.objects.filter(id=nid).delete()
    return redirect(\'get_teachers.html\')
 
 
def edit_teachers(request):
    if request.method == "GET":
        nid = request.GET.get(\'nid\')
        obj = models.Classes.objects.filter(id=nid).first()
        teachers_list = models.Teachers.objects.all()
        return render(request,\'edit_teachers.html\',{\'nid\':nid,\'obj\':obj,\'teachers_list\':teachers_list})
    elif request.method == "POST":
        n = request.POST.get(\'nid\')
        t = request.POST.get(\'titile\')
        teachers_id = request.POST.getlist(\'teachers_id\')
        obj = models.Classes.objects.filter(id=n).update(
            id=n,
            titile=t,
        )
        obj.m.add(*teachers_id)
        return redirect(\'get_teachers.html\')

添加功能

get_teachers.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <a href="/add_teachers.html">添加</a>
</div>
<div>
    <table border="1">
        <thead>
        <tr>
            <th>ID</th>
            <th>班级</th>
            <th>分配老师</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for row in cls_list %}
            <tr>
                <td>{{ row.id }}</td>
                <td>{{ row.titile }}</td>
                <td>
                    {% for cls in row.m.all %}
                    {{ cls.name }}
                    {% endfor %}
                </td>
                <td>
                    <a href="/del_teachers.html?nid={{ row.id }}">删除</a>
                    |
                    <a href="/edit_teachers.html?nid={{ row.id }}">编辑</a>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
</body>
</html>

添加功能

add_teachers.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="add_teachers.html" method="POST">
    {% csrf_token %}
    <p><input type="text" name="titile" placeholder="班级" /></p>
    <p>
    <select name="teachers_id" multiple="multiple" >
          {% for teachers in teachers_list %}
           <option value="{{ teachers.id}}">{{ teachers.name }}</option>
          {% endfor %}
    </select>
    </p>
    <input type="submit" value="提交" />
</form>
</body>
</html>

编辑功能

edit_teachers.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/edit_teachers.html?nid={{ nid }}" method="POST">
    {% csrf_token %}
     <p><input type="text" name="titile" placeholder="班级" value="{{ obj.titile }}"/></p>
    <p>
    <select name="teachers_id" multiple="multiple" >
          {% for teachers in teachers_list %}
           <option value="{{ teachers.id}}">{{ teachers.name }}</option>
          {% endfor %}
    </select>
    </p>
    <input type="submit" value="提交">
</form>
</body>
</html>

以上是关于django 多对多 增 删 改 查的主要内容,如果未能解决你的问题,请参考以下文章

Django 多对多关系的增删改查

Django数据库操作

Django多对多的增删改查

django数据库操作-增删改查-多对多关系以及一对多(外键)关系

转: django数据库操作-增删改查-多对多关系以及一对多(外键)关系

django一对多 增 删 改 查