Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

609
Views
How to update multiple objects in django

I'd like to update more than one objects at same time, when the register date achieve more than 6 days:

The Idea is update all issue_status from 'On Going' to 'Pending' for each objects

Is it necessary iterate it?

Below is my current code and error:

models.py
class MaintenanceIssue(models.Model):   
    issue_status = models.CharField(max_length=30, choices=[('pending', 'Pending'), ('on going', 
    'On going'), ('done', 'Done')])    
    register_dt = models.DateTimeField(blank=True, null=True) 

    @property
    def pending_issue(self):
        issue_time_diff = (datetime.now() - self.register_dt).days
        return issue_time_diff

views.py:

on_going_issues = MaintenanceIssue.objects.get(issue_status='On Going')
    if on_going_issues.pending_issue > 6:
        on_going_issues.issue_status = 'Pending'
        on_going_issues.save()

get() returned more than one MaintenanceIssue -- it returned 61!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

To update all objects in one go, you'll need to create a query that selects all objects you want to update then call update on it

MaintenanceIssue.objects.filter(
    issue_status='On Going',
    register_dt__lt=datetime.datetime.now() - datetime.timedelta(days=6)
).update(issue_status='Pending')

That filter does not exactly match your property, the following should get you a better match although it's a bit uglier

from django.db.models import F, Value
from django.db.models.functions import ExtractDay

MaintenanceIssue.objects.filter(
    issue_status='On Going'
).annotate(
    days=ExtractDay(Value(datetime.datetime.now()) - F('register_dt'))
).filter(
    days__gt=6
).update(issue_status='Pending')
over 4 years ago · Santiago Trujillo Report

0

at:

on_going_issues = MaintenanceIssue.objects.get(issue_status='On Going')
    if on_going_issues.pending_issue > 6:
        on_going_issues.issue_status = 'Pending'
        on_going_issues.save()

should filter by the field and then loop through each

on_going_issues = MaintenanceIssue.objects.filter(issue_status='On Going')
for one in on_going_issues:
   if one.pending_issue > 6:
       one.issue_status = "Pending"
       one.save()
over 4 years ago · Santiago Trujillo Report

0

you can do it with one line and it will be much faster

MaintenanceIssue.objects.filter(issue_status='On Going').update(issue_status = "Pending")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!