[Answer]-Django, create list of objects which can be mass deleted, edited etc

1👍

What you need is formset. https://docs.djangoproject.com/en/dev/topics/forms/formsets/

If you want to delete bunch of users you could:

1) Create Userform, which has one additional boolean field – delete

2) Change the form, that in save method, if delete equals True, you delete the object

3) Create formset, which uses previouscly created form:

UserFormSet = modelformset_factory(User, form=PreviouslyCreatedUserForm)

4) If in view formset cleans without errors call formset.save()

5) Give user some feedback.

Perhaps doing it in form save method is not ok, in that case you can also loop through the forms in view like. You would have to set some kind of parameter then though, to find out if form/object is marked for deletion.

for form in formset:
  if form.delete = True:
    obj = form.save(commit = False)
    obj.delete()

Anyway, formsets are very powerful tool, look into the subject and i am sure it will come up with different ways to use it.

Leave a comment