Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading