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

916
Visualizações
Difference between auto_now and auto_now_add

What I understood in Django models field's attributes is

  • auto_now - updates the value of field to current time and date every time the Model.save() is called.
  • auto_now_add - updates the value with the time and date of creation of record.

My question is what if a filed in model contains both the auto_now and auto_now_add set to True? What happens in that case?

over 4 years ago · Santiago Trujillo
5 Respostas
Responde à pergunta

0

These fields are built into Django for expressly this purpose — auto_now fields are updated to the current timestamp every time an object is saved and are therefore perfect for tracking when an object was last modified, while an auto_now_add field is saved as the current timestamp when a row is first added to the database and is therefore perfect for tracking when it was created.

over 4 years ago · Santiago Trujillo Relatório

0

According to the django documentation using both auto_now and auto_now_add in your model fields as True will result in an error because they are both mutually exclusive.

over 4 years ago · Santiago Trujillo Relatório

0

As Official Django documentation says -

auto_now, auto_now_add and default are mutually exclusive and will result in an error if used together

over 4 years ago · Santiago Trujillo Relatório

0

If a filed in a model contains both the auto_now and auto_now_add set to True will result in an error like this:

ERRORS: appname.Model.field_name: (fields.Efield_number) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.

over 4 years ago · Santiago Trujillo Relatório

0

auto_now takes precedence (obviously, because it updates field each time, while auto_now_add updates on creation only). Here is the code for DateField.pre_save method:

def pre_save(self, model_instance, add):
    if self.auto_now or (self.auto_now_add and add):
        value = datetime.date.today()
        setattr(model_instance, self.attname, value)
        return value
    else:
        return super().pre_save(model_instance, add)

As you can see, if auto_now is set or both auto_now_add is set and the object is new, the field will receive current day.

The same for DateTimeField.pre_save:

def pre_save(self, model_instance, add):
    if self.auto_now or (self.auto_now_add and add):
        value = timezone.now()
        setattr(model_instance, self.attname, value)
        return value
    else:
        return super().pre_save(model_instance, add)
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