gpt4 book ai didi

django - 如何删除 django-extra-views 中的行?

转载 作者:行者123 更新时间:2023-12-02 06:55:04 24 4
gpt4 key购买 nike

ModelFormSetView中如何删除行

这是我的代码,如何管理 Django-extra-views 中的删除行

我正在尝试使用 ModelFormSetView

if formset.deleted_forms:
for obj in formset.deleted_forms:
obj.delete()

HTML

{{ formset.management_form }}
{% for object in formset %}
{% for hidden in formset.hidden_fields %}
{{ hidden }}
{% endfor %}
<tr class="formset" class="even pointer">{{ object.id }}
<td class=" ">{{ object.as_p }}</td>
<td class=""></td>
</tr>
{% endfor %}

查看

这是一般 View ,我如何管理填写此内容的删除


class MeasurementPropsUpdateViews(ModelFormSetView):
model = MeasurementProps
form_class= MeasurementPropsForm

template_name = "master/measurementprops_form.html"
def get_queryset(self):
pk = self.kwargs.get('pk')
current_user = self.request.user
return self.model.objects.filter(ProductName=pk, user=current_user)

def get_success_url(self):
return reverse("tailoringproducts")

def formset_valid(self, formset):
for docs_form in formset:
docs_form.save(commit=False)
if formset.deleted_forms:
for obj in formset.deleted_forms:
obj.delete()
docs_form.instance.ProductName_id = self.kwargs.get('pk')
docs_form.instance.user = self.request.user
docs_form.save()
messages.success(self.request, "Measurement Properties Updated successfully")
return HttpResponseRedirect(self.get_success_url())

def formset_invalid(self, formset):
messages.error(self.request, "Form getting invalid")
return self.render_to_response(self.get_context_data(formset=formset))



最佳答案

我还没有对此进行测试,但根据文档,可以在类级别设置与表单集相关的属性。因此,根据您的情况,添加 factory_kwargs 并将 can_delete 指定为 True,例如:

class MeasurementPropsUpdateView(ModelFormSetView):
factory_kwargs = { 'can_order': False, 'can_delete': True}

Documentation

关于django - 如何删除 django-extra-views 中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56661748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com