码迷,mamicode.com
首页 > 其他好文 > 详细

forms组件04 formset批量编辑功能

时间:2019-10-16 13:37:46      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:return   ane   pid   body   传值   code   data   npm   tab   

默认值:

def multi_edit(request):
    form_class = formset_factory(MultiPermissionForm,extra=0) ##extra=0不额外生成
    if request.method == GET:
        formset = form_class(initial=[{title:xi,url:xxx}])  ####initial参数列表里放字典
        return render(request, multi_edit.html, locals())
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <style>

    </style>
</head>
<body>
<div class="container">
    <div class="col-md-offset-2">
        <form method="post">
        {% csrf_token %}
        {{ formset.management_form }}
            <table border="1">
                <thead border="2">
                <tr>
                    <td>title</td>
                    <td>url</td>
                    <td>name</td>
                    <td>menu_id</td>
                    <td>pid_id</td>
                </tr>
                </thead>
                <tbody>
                {% for form in formset %}
                    <tr>
                        {% for field in form %}
                            <td>{{ field }}<span style="color: red;float: right">{{ field.errors.0 }}</span></td>
                        {% endfor %}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        <input type="submit" value="提交">
        </form>
    </div>
</div>
</body>
</html>

技术图片

 

 

 


 

更新数据:

  form不能和添加的form一样 ===》 多一个id字段 用于判断更新哪条数据==》在前端渲染时隐藏id字段(不能让用户选择) forms.HiddenInput

class UpdateMultiPermissionForm(forms.Form):
    id = forms.IntegerField( 
        widget=forms.HiddenInput(attrs={class: form-control})   ########新增加id字段 并且设置隐藏
    )
    title = forms.CharField(
        widget=forms.TextInput(attrs={class:form-control})
    )
    url = forms.CharField(
        widget=forms.TextInput(attrs={class: form-control})
    )

    name = forms.CharField(
        widget=forms.TextInput(attrs={class:form-control})
    )
    menu_id = forms.ChoiceField(
        choices=[(None,------------)],
        widget=forms.Select(attrs={class: form-control}),
        required = False
    )
    pid_id = forms.ChoiceField(
        choices=[(None, -----------)],
        widget=forms.Select(attrs={class: form-control}),
        required=False
    )
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields[menu_id].choices += Menu.objects.values_list(id,title)
        self.fields[pid_id].choices += Permission.objects.filter(pid__isnull=True).exclude(
            menu__isnull = True
        ).values_list(id,title)
    {% for field in form %}                 #########第一个的话就不生成td 页面不会错开
       {% if forloop.first %}
          {{ field }}
       {% else %}
    <td>{{ field }}<span style="color: red;float: right">{{ field.errors.0 }}</span></td>
       {% endif %}
    {% endfor %}

 因为了id字段 所以后端传值要 加上id,然后用id来数据更新

  formset = form_class(initial=[{‘id‘:1,‘title‘:‘xi‘,‘url‘:‘xxx‘}]) ##models.Permission,objects.all().values(xxx,xx,xx)

def multi_edit(request):
    form_class = formset_factory(UpdateMultiPermissionForm,extra=0)##################
    if request.method == GET:
        formset = form_class(initial=Permission.objects.all().values(id,title,url,name,menu_id,pid_id))
        return render(request, multi_edit.html, locals())
    formset = form_class(data=request.POST)
    if formset.is_valid():
        post_row_list = formset.cleaned_data
        for i in range(0, formset.total_form_count()):
            try:
                Permission.objects.filter(id=post_row_list[i].get(id)).update(**post_row_list[i])
            except Exception as e:
                pass
    else:
        return render(request, multi_edit.html, locals())
    return render(request, multi_edit.html, locals())

 

 

  

 

forms组件04 formset批量编辑功能

标签:return   ane   pid   body   传值   code   data   npm   tab   

原文地址:https://www.cnblogs.com/Jnhnsnow/p/11684708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!