core.mixins.FilePathModelMixin module

Module with the core.mixins.FilePathModelMixin mixin.

class core.mixins.FilePathModelMixin.FilePathModelMixin(*args, **kwargs)[source]

Bases: Model

Mixin adding functionality for managing a single storage file for a model class.

file_path

The relative path in the storage where the file is stored. Can be null if no file has been saved (null does not collide with the unique constraint.).

class Meta[source]

Bases: object

Metadata class for the mixin, abstract to avoid makemigrations picking it up.

abstract = False
save(*args, **kwargs)[source]

Extended :django:django.models.Model.save() method.

Saves the data to storage if configured.

Return type:

None

Parameters:
_get_storage_file_name()[source]

Create the filename for the stored file.

Return type:

str

open_file(mode='rb')[source]

Opens and returns the stored file as a filestream.

Note

Use inside a with block.

Parameters:

mode (str) – The mode the file is opened in.

Return type:

File

Returns:

The filestream of the file.

Raises:

FileNotFoundError – If the file_path is not set or the file is not found in the storage.

delete_file()[source]

Deletes the file and sets file_path to None.

Intended for use in a signal.

Return type:

None

_meta = <Options for FilePathModelMixin>
property absolute_filepath: str | None

The absolute filepath in the server fs for use with open.

Returns:

The absolute path in the stored file. If no file_path is set, returns None.