在 Django 前端删除多行

发布于 2024-12-05 19:38:38 字数 236 浏览 1 评论 0原文

我将在 django 中实现多重删除(通过选择框)视图。

我知道 django.contrib.admin.actions 中有一个视图,但我无法将其移植到前端。

我应该在表单中分配对象 ID 并将它们发布到我的删除视图,然后使用 .delete() 吗?

我以前没有接触过编程,Django 是我开始编程冒险的框架。

我正在寻找示例(Django view + html),但找不到任何示例。

I am going to implment a multi delete (via select boxes) view in django.

I know there's a view in django.contrib.admin.actions but I can't port this to frontend.

Should I assign object id's in the form and POST these to my delete view and then use .delete() ?

I haven't been programming before, and Django is the framework I start my programming adventure.

I was looking for example (for Django view + html) but couldn't find any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-12-12 19:38:38

使用 modelformset并仅使用 delete 选项手动呈现表单:https://docs. djangoproject.com/en/dev/topics/forms/formsets/#manually-rendered-can-delete-and-can-order

替代解决方案:

使用 modelformset 创建一堆带有删除复选框的表单如下所示:

class YourModelForm(forms.ModelForm):
    id = fields.IntegerField(widget=widgets.HiddenInputField)
    delete = fields.BooleanField(required=False)

    def save(self, commit=False):
        if self.is_valid() and self.cleaned_data['delete']:
            self.instance.delete()             

    class Meta:
        model = YourModel

Using a modelformset and manually rendering the form with just the delete options: https://docs.djangoproject.com/en/dev/topics/forms/formsets/#manually-rendered-can-delete-and-can-order

Alternative solution:

Using a modelformset to create a bunch of forms with a delete checkbox like this:

class YourModelForm(forms.ModelForm):
    id = fields.IntegerField(widget=widgets.HiddenInputField)
    delete = fields.BooleanField(required=False)

    def save(self, commit=False):
        if self.is_valid() and self.cleaned_data['delete']:
            self.instance.delete()             

    class Meta:
        model = YourModel
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文