• 基于多对多字段的增删改查


    增:

    def add_users(request):
        if request.method == "POST":
            user_id = request.POST.getlist("add_user")
            text_users = request.POST.get("text_users")
            text_pwd = request.POST.get("text_pwd")
            new_id = models.User.objects.create(name=text_users, pwd=text_pwd)
            new_id.roles.add(*user_id)  # 多对多字段添加方法
            return redirect("/users/")
        add_obj = models.Role.objects.all()
        return render(request, "add_users.html", locals())

    删:

    def delete_users(request, id):
        models.User.objects.filter(id=id).delete()
        return redirect("/users/")

    改:

    def edeit_users(request, id):
        if request.method =="POST":
            user_id = request.POST.getlist("add_user")
            text_users = request.POST.get("text_users")
            text_pwd = request.POST.get("text_pwd")
            update_obj = models.User.objects.get(id=id)
            update_obj.name=text_users
            update_obj.pwd=text_pwd
            # 多对多用set
            update_obj.roles.set(user_id)
            update_obj.save()
            return redirect("/users/")
        
        user_text = models.User.objects.filter(id=id)
        roles_list = user_text.values_list("roles__id")
        roles_list_new = []
        for i in roles_list:
            roles_list_new.append(i[0])
        role_list = models.Role.objects.all()
        id = id
        return render(request, "edeit_users.html", locals())

    查:

    def users(request):
        user_list = models.User.objects.all()
    
        return render(request, "user.html", locals())
  • 相关阅读:
    773. 有效的字母异位词
    768. 杨辉三角
    759. 时间角度
    757. 最短无序数组
    749. 约翰的后花园
    737. 查找矩阵
    AIX如何点亮HBA卡
    Spring Boot 静态文件,请求不到,util文件夹
    Spring Boot 静态文件,请求不到,util文件夹
    Bootstrap表格组件 Bootstrap Table
  • 原文地址:https://www.cnblogs.com/Rivend/p/11699532.html
Copyright © 2020-2023  润新知