[英]AttributeError: 'QuerySet' object has no attribute 'save'
This is the page that I'm trying to work out.这是我正在努力解决的页面。 If the update is clicked, the filled-in details should be updated in a MySql database called TL.如果单击更新,则应在名为 TL 的 MySql 数据库中更新填写的详细信息。
But while clicking update, it's throwing the following error: AttributeError at /add/ 'QuerySet' object has no attribute 'save'但是在单击更新时,它会抛出以下错误: /add/ 'QuerySet' 对象的 AttributeError 没有属性 'save'
The following is Views.py file in Django where I had put code for add:以下是 Django 中的 Views.py 文件,我在其中放置了用于添加的代码:
def add(request):
ad = TL.objects.all()
if request.method == 'POST':
TL_Name = request.POST.get('TL_Name')
Proj_name = request.POST.get('Proj_name')
Deadline = request.POST.get('Deadline')
ad.TL_Name = TL_Name
ad.Proj_name = Proj_name
ad.Deadline = Deadline
ad.save()
return redirect("/operations")
return render(request, 'operations.html', {"name": ad, 'b': ad})
The following is the urls.py file:以下是 urls.py 文件:
from . import views
urlpatterns = [
path('', views.home),
path('adminlogin/', views.adminlogin),
path('operations/', views.operations),
path('approve/<int:pk>', views.approval),
path('decline/<int:pk>/', views.delete),
path('edit/<int:pk>/', views.edit),
path('add/', views.add),
path('tlist/', views.approved_tlist)
]
The following is the operations.html file:以下是operations.html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Team Leaders</title>
</head>
<body>
<h1>List of Team Leaders</h1>
<table id="table">
<tr>
<th>TL_Name</th>
<th>Proj_name</th>
<th>Proj_Status</th>
<th>Deadline</th>
<th>Action</th>
</tr>
{% for i in name %}
<tr>
<td>{{i.TL_Name}}</td>
<td>{{i.Proj_name}}</td>
<td>{{i.Proj_Status}}</td>
<td>{{i.Deadline}}</td>
<td>
<a href="/approve/{{i.id}}">Approve</a>
<a href="/decline/{{i.pk}}">Decline</a>
<a href="/edit/{{i.pk}}">Edit</a>
</td>
</tr>
{% endfor %}
</table>
<br>
<br>
{% if a %}
<form method="post">
{% csrf_token %}
<table>
<tr>
<td>
<label>TL_Name</label>
</td>
<td>
<input type="text" name="TL_Name" value="{{a.TL_Name}}">
</td>
</tr>
<tr>
<td>
<label>Proj_Name</label>
</td>
<td>
<input type="text" name="Proj_name" value="{{a.Proj_name}}">
</td>
</tr>
<tr>
<td>
<label>Proj_Status</label>
</td>
<td>
{{a.Proj_Status}}
</td>
</tr>
<tr>
<td>
<label>Deadline</label>
</td>
<td>
<input type="text" name="Deadline" value="{{a.Deadline}}">
</td>
</tr>
<tr>
<td>
<input type="submit" value="Update">
</td>
</tr>
</table>
</form>
{% endif %}
<tr>
<a href="/add/">Add</a>
</tr>
{% if b %}
<form method="post">
{% csrf_token %}
<table>
<tr>
<td>
<label>TL_Name</label>
</td>
<td>
<input type="text" name="TL_Name" value="{{b.TL_Name}}">
</td>
</tr>
<tr>
<td>
<label>Proj_Name</label>
</td>
<td>
<input type="text" name="Proj_name" value="{{b.Proj_name}}">
</td>
</tr>
<tr>
<td>
<label>Proj_Status</label>
</td>
<td>
{{b.Proj_Status}}
</td>
</tr>
<tr>
<td>
<label>Deadline</label>
</td>
<td>
<input type="text" name="Deadline" value="{{b.Deadline}}">
</td>
</tr>
<tr>
<td>
<input type="submit" value="Update">
</td>
</tr>
</table>
</form>
{% endif %}
</body>
</html>
Please help me to sort out this error.请帮我解决这个错误。 Thank you in advance...先感谢您...
ad = TL.objects.all()
is assigning the queryset of all TL to ad正在将所有 TL 的 queryset 分配给 ad
ad.TL_Name = TL_Name
ad.Proj_name = Proj_name
ad.Deadline = Deadline
ad.save()
cannot therefore be done as this isn't a single item.因此无法完成,因为这不是单个项目。
If you want update all objects of TL you can use update如果你想更新 TL 的所有对象,你可以使用 update
ad = TL.objects.update(TL_Name=TL_Name, Proj_name=Proj_name, Deadline=Deadline)
or use TL.objects.first() or TL.objects.get(id=id_you_want)或使用 TL.objects.first() 或 TL.objects.get(id=id_you_want)
to get an individual instance of the model and then use获取模型的单个实例,然后使用
ad.TL_Name = TL_Name
ad.Proj_name = Proj_name
ad.Deadline = Deadline
ad.save()
You first set ad = TL.objects.all()
This returns all your Model objects.您首先设置ad = TL.objects.all()
这将返回您的所有模型对象。 Then later on in your code you're trying to save ad
to your database.然后稍后在您的代码中,您尝试将ad
保存到您的数据库中。 That won't work, and Django is telling you that.那是行不通的,Django 正在告诉你。 You're trying to save a queryset.您正在尝试保存查询集。
You have this error because you apply a .save()
method on a queryset, that is wrong.您遇到此错误是因为您在查询集上应用了.save()
方法,这是错误的。 Instead you need to call .save()
on a object instance like this:相反,您需要在这样的对象实例上调用.save()
:
def add(request):
# ad = TL.objects.all() Not usefull here
context = {}
if request.method == 'POST':
TL_Name = request.POST.get('TL_Name')
Proj_name = request.POST.get('Proj_name')
Deadline = request.POST.get('Deadline')
# Create a new TL instance here (Note that with class.objects.create() we don't need to call save())
new_tl = TL.objects.create(TL_Name=TL_Name, Proj_name=Proj_name, Deadline=Deadline)
# Update the context data
context = ['b'] = new_tl
return redirect("/operations")
# Retrieve all TL objects and add to context
context['name'] = TL.objects.all()
return render(request, 'operations.html', context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.