使用 Django 一次更新多条记录 [英] Updating several records at once using Django
问题描述
我想在左侧创建一个带有复选框的记录列表......有点像 Gmail 中的收件箱.然后,如果用户选择了这些复选框中的一些或全部,则可以通过单击按钮来更新所选记录(顺便说一句,只会更新一个字段).
I want to create a list of records with checkboxes on the left side....kinda like the inbox in Gmail. Then if a user selects some or all of these checkboxes, then the selected record(s) can be updated (only one field will be updated BTW), possibly by clicking a button.
我被困在如何做到这一点上....想法?
I'm stuck on how to do this though....ideas?
显示代码
{% for issue in issues %}
<tr class="{% cycle 'row1' 'row2' %}">
<td><input name="" type="checkbox" value="{{ issue.id }}" /></td>
<td>{{ issue.description }}</td>
<td>{{ issue.type }}</td>
<td>{{ issue.status }}</td>
<td>{{ issue.date_time_added|date:"d, M Y" }}</td>
<td>{{ issue.added_by }}</td>
<td>{{ issue.assigned_to }}</td>
</tr>
{% endfor %}
推荐答案
id_list = list_of_ids_from_checkboxes
MyModel.objects.filter(id__in=id_list).update(myattribute=True)
您的显示 HTML 缺少复选框的 name
值.如果您在所有复选框中只有一个名称,那么 ID 列表将被传递到一个 POST 变量中,您可以直接从 request.POST
(假设您将表单提交为一个帖子,你应该是):
Your display HTML is missing a name
value for the checkboxes. If you just have a single name across all checkboxes, then the list of IDs will be passed into a single POST variable, which you can get straight from request.POST
(assuming you're submitting your form as a post, which you should be):
id_list = request.POST.getlist('checkboxname')
这篇关于使用 Django 一次更新多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!