Home > Back-end >  how to update all the record at once in Django
how to update all the record at once in Django

Time:11-09

I want to display all the created department in one view using a for loop and update all the record at once. the problem is it is taking only the first record and updating all the record with the value entered at the first field I tried using .getlist() but still taking only the first record. how to update each record with reference to its id and if the field is empty keep that record untouched.

Views.py

def department(request):
departments = Department.objects.all()
value1 = request.POST.getlist('BUID')
value2 = request.POST.getlist('GBUID')
print(value1)
print(value2)
Department.objects.update(
    BUID=request.POST.get('BUID'),
    GBUID=request.POST.get('GBUID'),
)
context = {'departments': departments}
return render(request, 'employee/department.html', context)

models.py

class Department(models.Model):
DepartmentID = models.IntegerField()        <------will be Entered by the admin
Name = models.CharField(max_length=200)     <------will be Entered by the admin
BUID = models.CharField(max_length=200, blank=True)    <------to be updated in the view
GBUID = models.CharField(max_length=200, blank=True)   <------to be updated in the view

templates

<form action="" method="POST">
{% csrf_token %}
    <h3>Manage Departments</h3>
    <input class="btn btn-primary" type="submit" value="Submit">

{% for department in departments %}
<div class="col-sm-6">
    <label>Department ID: {{department.DepartmentID}}</label>
</div>
<div class="col-sm-6">
    <label>Department Name: {{department.Name}}</label>
</div>

<label>BU/Department Manager Id:</label>
<div class="col-sm-3">
    <input name="BUID" class="form-control">
</div>

<label>GBU/Group Department Manager Id:</label>
<div class="col-sm-3">
    <input name="GBUID" class="form-control">
</div>

</form>
{% endfor %}

CodePudding user response:

You need to change views.py file :-

views.py

def department(request):
   departments = Department.objects.all()
   value1 = request.POST.getlist('BUID')
   value2 = request.POST.getlist('GBUID')
   print(value1)
   print(value2)
   Department.objects.all.update(
       BUID=value1,
       GBUID=value2
   )
   context = {'departments': departments}
   return render(request, 'employee/department.html', context)

CodePudding user response:

if you want to update many rows at same time you need to use the following https://docs.djangoproject.com/en/3.2/ref/models/querysets/#bulk-update

Note: bulk update will ignore signals which raised during object save

CodePudding user response:

views.py:

from django.http.request import HttpRequest
from django.db.models.query import QuerySet

def department(request: HttpRequest):
   departments: QuerySet[Department] = Department.objects.all()
   value1 = request.POST.getlist('BUID')
   value2 = request.POST.getlist('GBUID')

   for department in departments:
      setattr(department, 'BUID', value1)
      setattr(department, 'GBUID', value2)
      department.save()

   context = {'departments': departments}
   return render(request, 'employee/department.html', context)

Also I have added types, so you will have better understanding what is going on

  • Related