Home > database >  AttributeError: 'QuerySet' object has no attribute 'save'
AttributeError: 'QuerySet' object has no attribute 'save'

Time:11-13

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.

enter image description here

But while clicking update, it's throwing the following error: AttributeError at /add/ 'QuerySet' object has no attribute 'save'

enter image description here

The following is Views.py file in Django where I had put code for add:

    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:

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:

<!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...

CodePudding user response:

    ad = TL.objects.all() 

is assigning the queryset of all TL to 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

   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)

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()

CodePudding user response:

You first set ad = TL.objects.all() This returns all your Model objects. Then later on in your code you're trying to save ad to your database. That won't work, and Django is telling you that. You're trying to save a queryset.

CodePudding user response:

You have this error because you apply a .save() method on a queryset, that is wrong. Instead you need to call .save() on a object instance like this:

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)
  • Related