开发者

Django: Deleting user selected entries from a database

I have a Django app that displays a list of rows in a table to the user. Each row maps to an entry in a database. I want to let the user select the rows they would like deleting by adding a checkbox to the end of each row and a delete button ( similar to how gmail lets you delete multiple mail messages). I can't quite figure out how to write the view in terms of finding out which rows were selected and how to map these to the IDs of the entries that need deleting from the database. A simple code snippet showing how to do t开发者_JAVA百科his would be greatly appreciated.

UPDATE: I've found this code snippet that I think should do the trick


You can use the CheckboxSelectMultiple widget to auto-generate the corresponding HTML code so you don't have to do it manually.

You can define your form like so:

class UsersForm(forms.Form):
    users = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=[QuerySetIterator(Users.objects.all(), "", False)], label="")

Another advantage is that you also get validation for free.


Create a formset and pass can_delete = True to the constructor. Then, in the template,

{{formset}}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜