使用Django一次更新多条记录 [英] Updating several records at once using Django

查看:644
本文介绍了使用Django一次更新多条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个记录列表,并在左侧.... kinda复选框,就像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 %}


推荐答案

使用查询集 update()方法

id_list = list_of_ids_from_checkboxes
MyModel.objects.filter(id__in=id_list).update(myattribute=True)

您的显示HTML缺少<复选框的code> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆