QuerySet'对象没有'save'属性

2 投票
2 回答
7191 浏览
提问于 2025-04-18 09:25

我无法在数据库中保存更改。还有,如何通过链接调用一个视图呢?

我想在点击编辑链接时编辑数据,在点击保存链接时保存数据,在点击移动链接时更新数据。请帮帮我。

这是我的 views.py 文件:

def answer(request):
    info2 = Trans.objects.filter(transtype=8)
    sid = 0
if request.GET:
  sid = request.GET.get('sid')
bookdata = { "details" : info2, 'sid':int(sid) }
info2.save()
resp =  render_to_response("account/answer.html", bookdata, context_instance=Context(request))
return resp

HTML 模板:

{% load i18n %}

<!doctype html>
<html>
<body>
<table border="1" style="width:800px">
<form action=" " method="POST">
<input type=hidden value= {{ s.id }} name ='sid'>
<tr>
{% for s in details %} 
</tr>
<tr>
<td>   
       {{ s.script }}
  {% if s.id == sid %}
        <input name="script" value="{{ s.script }}" />

  {% endif %}
</td>

<td> <a href="/accounts/answer/?sid={{ s.id }}" name="edit">Edit</a> </td>
<td> <a href="/accounts/answer/?sid={{ s.id }}" name="save">Save</a> </td>
<td> <a href="/accounts/answer/?sid={{ s.id }}" name="move">Move</a> </td>
{% endfor %} 

</table>

</body>
</html>

Models.py 文件:

class Trans(models.Model):
    transtype = models.ForeignKey(TransType)
    title = models.CharField(max_length=200)
    script = models.CharField(max_length=200)
    movestatus= models.SmallIntegerField(default = 0)
    created = models.DateTimeField(auto_now_add = True)
    updated = models.DateTimeField(auto_now = True) 
    class Meta:
          unique_together = (("transtype", "script"),)
    def __unicode__(self):
    return unicode(self.transtype)

2 个回答

-2

你应该把这一行 info2 = Trans.objects.filter(transtype=8) 替换成这一行 info2=Trans.objects.get(transtype=8)

3

你需要使用update()这个方法,因为你正在处理的是一个查询集(也就是多个对象)

可以在这里查看示例: https://docs.djangoproject.com/en/dev/topics/db/queries/#updating-multiple-objects-at-once

撰写回答