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

278
Views
Update only that property not all model, django
@property
def policy_escape(self):
    if self.date - datetime.date.today:
        self.status = 2
        self.save()

def save(self, *args, **kwargs):
   self.counter = counter + 1

I have this logic for policies. For every policy created I want to increase a property nnumber(this is not exact my case).

I want when policy date is today date to change the status of that policy. But when I change it, the policy counter is increasing.

Can someone help me to update only status without updating other properties?

Thanks in advance

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could limit the update_fields:

@property
def policy_escape(self):
    if self.date - datetime.date.today:
        self.status = 2
        self.save(update_fields=("status",))

However, the particular Python object would still have the changed counter. So you would have to avoid any other save calls on that object.

You could instead pass a custom kwarg:

@property
def policy_escape(self):
    if self.date - datetime.date.today:
        self.status = 2
        self.save(update_counter=False)

def save(self, *args, **kwargs, update_counter=True):
    if update_counter:
        self.counter += 1
    # ...
    super().save(*args, **kwargs):

Another way to avoid save being called at all is using Queryset.update instead:

@property
def policy_escape(self):
    if self.date - datetime.date.today:
        self.__class__.objects.filter(pk=self.pk).update(status=2)
        # self.status = 2  # if the current object should reflect that change
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!