zoukankan      html  css  js  c++  java
  • Django使用ORM之多对多(四)

    上篇随笔讲了Django使用ORM之一对多实现书籍及书籍对应的出版社的增删查改。

    由于作者和书籍之间是多对多的关系,那么怎么实现多对多的ORM操作数据库增删查改呢。

    1、首先在models.py中创建作者类

    1 class Author(models.Model):
    2     id = models.AutoField(primary_key=True)
    3     name = models.CharField(max_length=64,null=False,unique=True)
    4     book = models.ManyToManyField(to=BoolInfo) #书籍和作者间是多对多的关系
    5     def __str__(self):
    6         return "<Author Object: {}>".format(self.name)

     2、在urls.py中加入作者的相关路径

    1 #作者对应关系
    2     url(r'^author_list/', views.author_list),   #作者列表信息
    3     url(r'^add_author/', views.add_author),     #添加作者信息
    4     url(r'^delete_author/', views.delete_author),   #删除作者信息
    5     url(r'^edit_author/', views.edit_author),       #编辑作者信息

     3、在views.py中编写对应的函数操作

     1 def author_list(request):   #获取作者列表
     2     #author=models.Author.objects.get(id=1)
     3     #print(author.book.all())
     4     all_author=models.Author.objects.all()  #获取所有的作者对象
     5     return render(request,"Author_List.html",{"author_list":all_author})    #返回作者列表页面,并在HTML页面以author_list变量替换所有作者变量
     6 
     7 def add_author(request):    #新增作者
     8     if request.method=="POST":  #判断若请求为POST方法
     9         new_author_name=request.POST.get("author_name")     #获取新的作者姓名
    10         book_list=request.POST.getlist("books")     #将书籍清单列表的id
    11         #print(new_author_name,book_list)
    12         new_author_obj=models.Author.objects.create(name=new_author_name)   #新增作者姓名
    13         new_author_obj.book.set(book_list)      #新增作者对象的书籍清单
    14         return redirect("/author_list/")        #重定向到作者列表页面
    15 
    16     ret=models.BoolInfo.objects.all()   #获取所有书籍信息
    17     return render(request,"Add_Author.html",{"all_book":ret})    #返回新增作者页面,并在HTML页面以all_book变量替换所有书籍变量
    18 
    19 def delete_author(request):     #删除作者
    20     delete_id=request.GET.get("id")     #获取前台页面传过来要删除的作者id
    21     models.Author.objects.get(id=delete_id).delete()    #执行删除操作
    22 
    23     # print(delete_id)
    24     return redirect("/author_list/")    #重定向到作者列表页面
    25 
    26 def edit_author(request):       #编辑作者
    27     # 若请求方式为POST
    28     if request.method=="POST":  
    29         edit_author_name = request.POST.get("author_name")  #获取编辑后的作者姓名
    30         book_list = request.POST.getlist("books")       #获取作者的书籍列表
    31         author_id=request.POST.get("author_id")     #获取要修改的作者id
    32         author_obj=models.Author.objects.get(id=author_id)      #获取要修改的作者对象
    33         # 执行修改并保存
    34         author_obj.name=edit_author_name   
    35         author_obj.book.set(book_list)  #一对多操作
    36         author_obj.save()
    37         return redirect("/author_list/")
    38     # 若请求方式为GET,获取要编辑书籍的信息并返回到前端页面
    39     edit_id=request.GET.get("id")
    40     author=models.Author.objects.get(id=edit_id)
    41     ret=models.BoolInfo.objects.all()
    42     return render(request,"Edit_Author.html",{"all_book":ret,"author":author})

     4、编写作者列表页面

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>作者列表</title>
     6 </head>
     7 <body>
     8     <h1>全部列表</h1>
     9     <table border="1">
    10         <thead>
    11             <tr>
    12                 <th>序号</th>
    13                 <th>作者id</th>
    14                 <th>作者名称</th>
    15                 <th>作品</th>
    16                 <th>操作</th>
    17             </tr>
    18         </thead>
    19         <tbody>
    20             {% for author in author_list %}
    21              <tr>
    22             <th>{{ forloop.counter }}</th>
    23             <th>{{ author.id }}</th>
    24             <th>{{ author.name }}</th>
    25                  <th>
    26                      {% for book in author.book.all %}
    27                         {{ book.title }}
    28                      {% endfor %}
    29                  </th>
    30              <th>
    31                  <a href="/delete_author/?id={{ author.id }}">删除</a>
    32                  <a href="/edit_author/?id={{ author.id }}">编辑</a>
    33              </th>
    34              </tr>
    35             {% endfor %}
    36         </tbody>
    37 
    38     </table>
    39     <a href="/add_author/">添加作者</a>
    40 </body>
    41 </html>

       删除和编辑操作中的url返回带有要操作作者的id

        <a href="/delete_author/?id={{ author.id }}">删除</a>
        <a href="/edit_author/?id={{ author.id }}">编辑</a>

     5、编写增加作者页面

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>添加作者</title>
     6 </head>
     7 <body>
     8     <form action="/add_author/" method="post">
     9         <p>
    10             作者名称:<input type="text" name="author_name">
    11         </p>
    12         <p>
    13             作品:<select multiple name="books">
    14                     {% for book in all_book %}
    15                         <option value="{{ book.id }}">{{ book.title }}</option>
    16                     {% endfor %}
    17 
    18                 </select>
    19         </p>
    20         <p>
    21             <input type="submit" value="提交">
    22         </p>
    23     </form>
    24 </body>
    25 </html>

    6、编写编辑作者页面

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>编辑作者</title>
     6 </head>
     7 <body>
     8     <form action="/edit_author/" method="post">
     9         <input type="text" name="author_id" value="{{ author.id }}" style="display: none">
    10         <p>
    11             作者名称:<input type="text" name="author_name" value="{{ author.name }}">
    12         </p>
    13         <p>
    14             作品:<select multiple name="books">
    15                     {% for book in all_book %}
    16                         {% if book in author.book.all %}
    17                             <option selected value="{{ book.id }}">{{ book.title }}</option>
    18                         {% else %}
    19                              <option value="{{ book.id }}">{{ book.title }}</option>
    20                         {% endif %}
    21 
    22                     {% endfor %}
    23 
    24                 </select>
    25         </p>
    26         <p>
    27             <input type="submit" value="提交">
    28         </p>
    29     </form>
    30 </body>
    31 </html>
  • 相关阅读:
    【BZOJ1486】最小圈(分数规划)
    【BZOJ4753】最佳团体(分数规划,动态规划)
    【POJ3621】【洛谷2868】Sightseeing Cows(分数规划)
    【POJ2728】Desert King(分数规划)
    【POJ2976】Dropping Tests(分数规划)
    【BZOJ4520】K远点对(KD-Tree)
    【BZOJ3991】寻宝游戏(动态规划)
    【BZOJ4066】简单题(KD-Tree)
    KD-Tree
    【BZOJ2648】SJY摆棋子(KD-Tree)
  • 原文地址:https://www.cnblogs.com/newway644617704/p/12772637.html
Copyright © 2011-2022 走看看