博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django 多对多 增 删 改 查
阅读量:4953 次
发布时间:2019-06-12

本文共 4984 字,大约阅读时间需要 16 分钟。

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

代码目录:

urls.py

"""django_one URL ConfigurationThe `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 urlfrom django.contrib import adminfrom app01.views import classesfrom app01.views import studentsfrom app01.views import teachersurlpatterns = [    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

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

from django.shortcuts import renderfrom django.shortcuts import redirectfrom app01 import modelsdef 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

    
Title
{% for row in cls_list %}
{% endfor %}
ID 班级 分配老师 操作
{
{ row.id }}
{
{ row.titile }}
{% for cls in row.m.all %} {
{ cls.name }} {% endfor %}
删除 | 编辑

添加功能

add_teachers.html

    
Title
{% csrf_token %}

编辑功能

edit_teachers.html

    
Title
{% csrf_token %}

 

转载于:https://www.cnblogs.com/nulige/p/6550789.html

你可能感兴趣的文章
如何迅速成为Java高手[Tomjava原创]
查看>>
《白帽子讲Web安全》- 学习笔记
查看>>
Python的ConfigParser模块读取ini配置文件 报错(持续更新总结)
查看>>
JavaMail转发邮件
查看>>
基于react-native实现的博客园手机客户端强势升级
查看>>
ASP.NET应用程序和ASP.NET网站所共有的文件: App_Browsers 等
查看>>
ASP.NET杂货店实战视频 VS2010+SQL2008 三层架构设计开发讲解
查看>>
样板操作数
查看>>
64位UBUNTU下安装adobe reader后无法启动
查看>>
动态缓存技术之CSI,SSI,ESI
查看>>
16级第二周寒假作业H题
查看>>
mac 上将.pem文件转为.pub文件
查看>>
整理下心情
查看>>
iTextSharp带中文转换出来的PDF文档显示乱码
查看>>
阶乘因式分解(一)
查看>>
qt学习记录-----3.信号与槽的问题
查看>>
『ORACLE』 内置约束(11g)
查看>>
Vue--学习过程中遇到的坑
查看>>
负载均衡之---应用请求路由模块的使用(ARR)(九)[在应用程序服务器上为HostNameMemory亲和提供程序配置WMI服务]...
查看>>
组件:slot插槽
查看>>