Fields

django_cryptography.fields.encrypt(base_field, key=None, ttl=None)[source]

A decorator for creating encrypted model fields.

Parameters:
  • key (bytes) –

    This is an optional argument.

    Allows for specifying an instance specific encryption key.

  • ttl (int) –

    This is an optional argument.

    The amount of time in seconds that a value can be stored for. If the time to live of the data has passed, it will become unreadable. The expired value will return an Expired object.

Return type:

models.Field[EncryptedMixin, T]

class django_cryptography.fields.PickledField(*args, **kwargs)[source]

A field for storing pickled objects

deconstruct()[source]

Return enough information to recreate the field as a 4-tuple:

  • The name of the field on the model, if contribute_to_class() has been run.
  • The import path of the field, including the class:e.g. django.db.models.IntegerField This should be the most portable version, so less specific may be better.
  • A list of positional arguments.
  • A dict of keyword arguments.

Note that the positional or keyword arguments must contain values of the following types (including inner values of collection types):

  • None, bool, str, int, float, complex, set, frozenset, list, tuple, dict
  • UUID
  • datetime.datetime (naive), datetime.date
  • top-level classes, top-level functions - will be referenced by their full import path
  • Storage instances - these have their own deconstruct() method

This is because the values here must be serialized into a text format (possibly new Python code, possibly JSON) and these are the only types with encoding handlers defined.

There’s no need to return the exact way the field was instantiated this time, just ensure that the resulting field is the same - prefer keyword arguments over positional ones, and omit parameters with their default values.

get_db_prep_value(value, connection, prepared=False)[source]

Return field’s value prepared for interacting with the database backend.

Used by the default implementations of get_db_prep_save().

get_default()[source]

Return the default value for this field.

to_python(value)[source]

Convert the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can’t be converted. Return the converted value. Subclasses should override this.

value_to_string(obj)[source]

Pickled data is serialized as base64

Constants

django_cryptography.fields.Expired = <object object>

Represents an expired encryption value.

Helpers

django_cryptography.fields.get_encrypted_field(base_class)[source]

A get or create method for encrypted fields, we cache the field in the module to avoid recreation. This also allows us to always return the same class reference for a field.

Return type:models.Field[EncryptedMixin, T]
class django_cryptography.fields.EncryptedMixin(*args, **kwargs)[source]

A field mixin storing encrypted data

Parameters:
  • key (bytes) –

    This is an optional argument.

    Allows for specifying an instance specific encryption key.

  • ttl (int) –

    This is an optional argument.

    The amount of time in seconds that a value can be stored for. If the time to live of the data has passed, it will become unreadable. The expired value will return an Expired object.

get_db_prep_save(value, connection)

Return field’s value prepared for saving into a database.