Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
Django how to over ride created date

We have a base model that sets a created and modified field:

class BaseModel(models.Model):
    created = models.DateTimeField(_('created'), auto_now_add=True)
    modified = models.DateTimeField(_('modified'), auto_now=True)
    ... other default properties

    class Meta:
        abstract = True

We use this class to extend our models:

class Event(BaseModel):

Is there a way to over ride the created date when creating new Events?

This is a stripped down version of our code. We are sending an array of event objects containing a created timestamp in our request payload. After the objects are added to the db, the created property is set to now and not the value from the payload.

I would like to still extend from the BaseModel as other areas of the code may not explicitly set a created value, in which case it should default to now.

events = []
for e in payload['events']:
    event = Event(
       created=datetime.datetime.fromisoformat(e['created'])
       name='foo'
    )
    events.append(event)
Event.objects.bulk_create(events)
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can override the created field for your Event model with:

from django.utils.timezone import now

class Event(BaseModel):
    created = models.DateTimeField(default=now)

If you do not want this to show up for ModelForms and ModelAdmins by default, you can make use of editable=False [Django-doc]:

from django.utils.timezone import now

class Event(BaseModel):
    created = models.DateTimeField(default=now, editable=False)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda