Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda