Deleting multiple rows in Django frontend

白昼怎懂夜的黑 提交于 2020-01-17 07:57:31

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/7496551/deleting-multiple-rows-in-django-frontend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!