DateTimeField - Django Models

Last Updated : 16 Feb, 2026

DateTimeField is a Django model field used to store both date and time values.

  • Represented in Python as a datetime.datetime object.
  • Stores a combined date and time value in the database.
  • The default form widget is DateTimeInput.
  • In Django Admin, it is displayed as two separate inputs (date and time) with JavaScript shortcuts.

Syntax

field_name = models.DateTimeField(**options)

Optional Arguments for DateTimeField

DateTimeField provides some additional useful options:

1. auto_now=True

Automatically sets the field to the current date and time every time the object is saved.

This is commonly used for "last modified" timestamps.

updated_at = models.DateTimeField(auto_now=True)

  • The current date/time is always used, it is not just a default value.
  • You cannot override it manually.
  • The field updates only when calling Model.save().
  • It does NOT update when using QuerySet.update() unless you explicitly set it.

Example:

Model.objects.filter(id=1).update(updated_at=timezone.now())

2. auto_now_add=True

Automatically sets the field to the current date and time when the object is first created. This is typically used for creation timestamps.

created_at = models.DateTimeField(auto_now_add=True)

  • The value is set only once during creation.
  • Even if you provide a value while creating the object, it will be ignored.
  • You cannot manually override it.

3. Using default

To keep the field editable or manually modifiable, use default instead of auto_now_add.

from django.utils import timezone
published_at = models.DateTimeField(default=timezone.now)

This is the recommended approach in modern Django projects because it respects USE_TZ=True.

Note: The options auto_now_add, auto_now, and default are mutually exclusive. Any combination of these options will result in an error.

Django Model DateTimeField Example

Consider a project named geeksforgeeks having an app named geeks.

In models.py:

Python
from django.db import models

class GeeksModel(models.Model):
    geeks_field = models.DateTimeField()

    def __str__(self):
        return str(self.geeks_field)

After running makemigrations and migrate, Django creates the corresponding database table with a DateTime column that stores both date and time values.

Opening Django Shell

Validation can be tested interactively by opening the Django shell:

python manage.py shell

Creating an Instance in Shell

Since modern Django projects usually have USE_TZ = True, it is recommended to use timezone-aware datetime values.

from geeks.models import GeeksModel
from django.utils import timezone

GeeksModel.objects.create(
geeks_field=timezone.now()
)

1
Creating a DateTimeField Instance Using Django Shell

Stored objects can be retrieved using:

GeeksModel.objects.all()

2
Fetching All GeeksModel Objects

This confirms that the DateTimeField successfully stores a datetime.datetime object in the database.

Validation Using Django Admin

The DateTimeField can also be validated using the Django Admin interface.

Register the Model in geeks/admin.py:

Python
from django.contrib import admin
from .models import GeeksModel

admin.site.register(GeeksModel)

Create a Superuser:

Enter username, email, and password when prompted.

python manage.py createsuperuser

Run the Development Server:

python manage.py runserver

Visit: http://127.0.0.1:8000/admin/ and log in using the superuser credentials, then navigate to the Geeks models section.

1
Viewing Stored DateTimeField Records in Django Admin

New instances can be added from this interface, and saved records can be viewed directly within the admin panel.

Field Options

Field options are arguments provided to a model field to apply constraints or define specific behavior for that field. For example, adding the argument null=True to a DateTimeField allows the corresponding database column to store NULL values in a relational database.
Below are the common field options and attributes that a DateTimeField can use:

Field OptionsDescription
NullIf True, Django will store empty values as NULL in the database. Default is False.
BlankIf True, the field is allowed to be blank. Default is False.
db_columnThe name of the database column to use for this field. If this isn't given, Django will use the field's name.
DefaultThe default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
help_textExtra "help" text to be displayed with the form widget. It's useful for documentation even if your field isn't used on a form.
primary_keyIf True, this field is the primary key for the model.
editableIf False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True.
error_messagesThe error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.
help_textExtra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.
verbose_nameA human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces.
validatorsA list of validators to run for this field. See the validators documentation for more information.
UniqueIf True, this field must be unique throughout the table.
Comment

Explore