Skip to main content

Command

This class provides functionality to install named fixtures into a database. It supports loading serialized data from multiple file formats and compression types, handling database transactions, and resetting database sequences. Users can specify target databases, exclude specific apps or models, and ignore non-existent fields during the import process.

Attributes

AttributeTypeDescription
helpstring = "Installs the named fixture(s) in the database."Installs the named fixture(s) in the database.
missing_args_messagestring = "No database fixture specified. Please provide the path of at least one fixture in the command line."No database fixture specified. Please provide the path of at least one fixture in the command line.

Constructor

Signature

def Command()

Methods


add_arguments()

@classmethod
def add_arguments(
parser: [CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser)
)

Defines the command-line arguments for the loaddata command, including fixture labels, database selection, and exclusion filters.

Parameters

NameTypeDescription
parser[CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser)The argument parser instance used to register command-line options and positional arguments.

handle()

@classmethod
def handle(
*fixture_labels: str,
**options: dict
)

Executes the command logic by initializing configuration from options and invoking the fixture loading process within a transaction.

Parameters

NameTypeDescription
*fixture_labelsstrOne or more names or paths of fixture files to be loaded into the database.
**optionsdictConfiguration options including database alias, verbosity, and exclusion rules.

compression_formats()

@classmethod
def compression_formats() - > dict

A dict mapping format names to (open function, mode arg) tuples.

Returns

TypeDescription
dictA mapping of file extensions like 'gz' or 'zip' to their respective file handler functions and access modes.

reset_sequences()

@classmethod
def reset_sequences(
connection: [BaseDatabaseWrapper](../../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper),
models: set
)

Reset database sequences for the given connection and models.

Parameters

NameTypeDescription
connection[BaseDatabaseWrapper](../../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper)The active database connection where sequences need to be updated.
modelssetA collection of model classes whose primary key sequences should be synchronized with the loaded data.

loaddata()

@classmethod
def loaddata(
fixture_labels: list
)

Orchestrates the discovery and loading of all specified fixtures while managing database constraints and sequence resets.

Parameters

NameTypeDescription
fixture_labelslistThe list of fixture identifiers provided by the user to be located and processed.

save_obj()

@classmethod
def save_obj(
obj: [DeserializedObject](../../../serializers/base/deserializedobject.md?sid=django_core_serializers_base_deserializedobject)
) - > bool

Save an object if permitted.

Parameters

NameTypeDescription
obj[DeserializedObject](../../../serializers/base/deserializedobject.md?sid=django_core_serializers_base_deserializedobject)The deserialized model instance ready to be persisted to the database.

Returns

TypeDescription
boolTrue if the object was successfully saved to the database, False if it was excluded by configuration.

load_label()

@classmethod
def load_label(
fixture_label: str
)

Load fixtures files for a given label.

Parameters

NameTypeDescription
fixture_labelstrThe specific label or path used to find and deserialize fixture data.

get_fixture_name_and_dirs()

@classmethod
def get_fixture_name_and_dirs(
fixture_name: str
) - > tuple

Determines the base filename and the list of directories to search based on whether the fixture name is an absolute or relative path.

Parameters

NameTypeDescription
fixture_namestrThe name or path of the fixture to resolve.

Returns

TypeDescription
tupleA tuple containing the base fixture name and a list of directory paths to search.

get_targets()

@classmethod
def get_targets(
fixture_name: str,
ser_fmt: str,
cmp_fmt: str
) - > set

Generates a set of potential file names for a fixture by combining the base name with supported database, serialization, and compression extensions.

Parameters

NameTypeDescription
fixture_namestrThe base name of the fixture file.
ser_fmtstrThe specific serialization format to target, or None to search all formats.
cmp_fmtstrThe specific compression format to target, or None to search all formats.

Returns

TypeDescription
setA set of strings representing all valid file name combinations for the fixture.

find_fixture_files_in_dir()

@classmethod
def find_fixture_files_in_dir(
fixture_dir: str,
fixture_name: str,
targets: set
) - > list

Searches a specific directory for files that match the targeted fixture names.

Parameters

NameTypeDescription
fixture_dirstrThe directory path to search within.
fixture_namestrThe base name of the fixture being sought.
targetssetThe set of valid filenames (including extensions) to look for.

Returns

TypeDescription
listA list of tuples containing the full path, directory, and fixture name for each match found.

find_fixtures()

@classmethod
def find_fixtures(
fixture_label: str
) - > list

Find fixture files for a given label.

Parameters

NameTypeDescription
fixture_labelstrThe label used to identify and locate fixture files across search directories.

Returns

TypeDescription
listA list of tuples containing the file path, directory, and name for all discovered fixtures matching the label.

fixture_dirs()

@classmethod
def fixture_dirs() - > list

Return a list of fixture directories. The list contains the 'fixtures' subdirectory of each installed application, if it exists, the directories in FIXTURE_DIRS, and the current directory.

Returns

TypeDescription
listA list of absolute directory paths where fixtures are searched for.

parse_name()

@classmethod
def parse_name(
fixture_name: str
) - > tuple

Split fixture name in name, serialization format, compression format.

Parameters

NameTypeDescription
fixture_namestrThe raw filename or label to be decomposed into its constituent parts.

Returns

TypeDescription
tupleA tuple of (name, serialization_format, compression_format) extracted from the input string.