Jump to >


Field for managing modification timestamps for a model.

class ModificationState[source]

Bases: object

Modification state for a tracked value.


bool – Whether or not the next setattr() will be the first.

When this is True, we do not consider the next setattr() to be a modification since the field will be set during model initialization.


bool – Whether or not the value has been manually modified.


Initialize the state.

static get_attr_name()[source]

Return the attribute name for the state for the named field.

Parameters:field_name (unicode) – The attribute name of the ModificationTimestampField.
Returns:The name of the modification state attribute.
Return type:unicode
class ModificationTrackedValue(field_name, state_name)[source]

Bases: object

A descriptor for tracking the modification of a value.


unicode – The attribute name of the field on the model.


unicode – The attribute name of the state on the model.

__init__(field_name, state_name)[source]

Initialize the descriptor.

  • field_name (unicode) – The name of the field attribute.
  • state_name (unicode) – The name of the state attribute.
__get__(instance, objtype=None)[source]

Return the value.


The value.

Return type:


__set__(instance, value)[source]

Set the value.

This tracks modifications and updates the state if this is not the first call to this method (which occurs during model initialization).

class ModificationTimestampField(*args, **kwargs)[source]

Bases: django.db.models.fields.DateTimeField

A timestamp field that only updates existing objects or when None.

This is a subclass of DateTimeField that only auto-updates the timestamp when updating an existing object or when the value of the field is None. It’s similar to using auto_now=True, but custom timestamp values on new instances will not be replaced.

__init__(*args, **kwargs)[source]

Initialize the field.

  • *args (tuple) – Positional arguments for the field.
  • **kwargs (dict) – Additional keyword arguments for the field.
contribute_to_class(cls, name)[source]

Contribute the field attributes to the class.

  • cls (type) – The model class to contribute the field to.
  • name (unicode) – The name of the field.
pre_save(model_instance, add)[source]

Return the value of the field just before saving.

  • model_instance (django.db.models.Model) – The model instance being saved.
  • add (bool) – Whether this is being saved to the database for the first time.

The date/time value being saved.

Return type: