'查询集'对象没有属性' save'

时间:2014-06-11 11:42:43

标签: python html django

我无法保存数据库中的更改。以及如何使用链接调用视图。 我想编辑编辑链接的数据,保存数据点击保存链接,更新数据点击移动链接。请帮助。

这是我的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 个答案:

答案 0 :(得分:0)

您需要使用update()方法,因为您正在处理查询集(多个对象)

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

答案 1 :(得分:-2)

您应将此行info2 = Trans.objects.filter(transtype=8)替换为info2=Trans.objects.get(transtype=8)