从django formset删除表单 [英] Deleting form from django formset

查看:444
本文介绍了从django formset删除表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试实现一个django formset(用户可以从formset中动态添加/删除表单)我使用JS添加新行(使用empty_form) :$($)

  $(#add-item)点击(function(e){
e.preventDefault );
var count = parseInt($('#id_form-TOTAL_FORMS')。val());
$('。invoice-items')。append($('#empty_invoice_item')。 html()。replace(/ __前缀__ / g,count));
$('#id_form-TOTAL_FORMS')attr('value',count + 1);
$(项目.invoice-item .col-lg-9 .form-group:last-child)。last()。append('< a href =#class =delete-item>< i class =glyphicon glyphicon-remove>< / i>< / a>')
});

我还使用JS在特定表单上设置DELETE标志。一切都传递给观点。



我的视图(部分)代码:

  invoice_form = InvoiceForm()
invoice_item_helper = InvoiceItemHelper
InvoiceItemFormset = formset_factory(InvoiceItemForm,extra = 0,max_num = 15,validate_max = True,min_num = 1,validate_min = True,can_delete = True)
formset = InvoiceItemFormset()

如果request.method =='POST':
invoice_form = InvoiceForm(request.POST)
formset = InvoiceItemFormset(request.POST)

问题是,django始终显示表单中的所有表单,甚至标记为删除的表单。所以,即使我的发票表单有问题,也没有验证,它会显示发票表单,并附上错误消息和所有表单(再次)。



如果request.method =='POST':阻止,我可以删除在中标记为删除的完整表单?这是可能的吗?

解决方案

这取决于您如何呈现表单,但您可以检查字段
form.DELETE ,如果设置,将该表单隐藏以进行显示,并且数据将被传递,直到数据被处理(当所有其他表单都有效时)。它还将确保表单的前缀和索引是完整的。



当表单集验证时,它将忽略标记为删除的表单。 formset.is_valid



您还可以使用 deleted_forms ,也可能是处理它们,但是您仍然需要重新构建整个表单,而不需要删除表单来维护索引和表单数量。
我个人发现这样做很复杂,导致复杂的代码。


I am trying to implement a django formset (where user may dynamically add/remove forms from formset).

I use JS to add new rows (using empty_form):

    $("#add-item").click(function(e){
        e.preventDefault();
        var count = parseInt($('#id_form-TOTAL_FORMS').val());
        $('.invoice-items').append($('#empty_invoice_item').html().replace(/__prefix__/g, count));
        $('#id_form-TOTAL_FORMS').attr('value', count+1);
        $(".invoice-items .invoice-item .col-lg-9 .form-group:last-child").last().append('<a href="#" class="delete-item"><i class="glyphicon glyphicon-remove"></i></a>')
    });

I also use JS to set DELETE flag on specific forms. Everything is passed to the view.

My view (part) code:

invoice_form = InvoiceForm()
invoice_item_helper = InvoiceItemHelper
InvoiceItemFormset = formset_factory(InvoiceItemForm, extra=0, max_num=15, validate_max=True, min_num=1, validate_min=True, can_delete=True)
formset = InvoiceItemFormset()

if request.method == 'POST':
    invoice_form = InvoiceForm(request.POST)
    formset = InvoiceItemFormset(request.POST)

The problem is, django always displays all forms in the formset, even those marked for deletion. So, even there is something wrong in my invoice form and it doesn't validate, it will show invoice form with error message AND all forms (once again).

How can I remove completely forms which are marked for deletion in if request.method == 'POST': block? Is it possible?

解决方案

It's dependent on how you render the forms, but you can check the field form.DELETE in the template and if it's set, render that form hidden for display and the data will be passed along until the data is processed (when all other forms are valid). It will also make sure form prefixes and indexes for the formset is intact.

When a formset is validated it will ignore forms marked for deletion. formset.is_valid

You can also pick up which forms are deleted in the view using deleted_forms and perhaps process them, still you will have to rebuild the whole formset without the deleted forms to maintain indexes and the count of forms. I personally found out that doing that is complex and leads to complicated code.

这篇关于从django formset删除表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆