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
| Attribute | Type | Description |
|---|
| help | string = "Installs the named fixture(s) in the database." | Installs the named fixture(s) in the database. |
| missing_args_message | string = "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
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
| Name | Type | Description |
|---|
| 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
| Name | Type | Description |
|---|
| *fixture_labels | str | One or more names or paths of fixture files to be loaded into the database. |
| **options | dict | Configuration options including database alias, verbosity, and exclusion rules. |
@classmethod
def compression_formats() - > dict
A dict mapping format names to (open function, mode arg) tuples.
Returns
| Type | Description |
|---|
dict | A 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
| Name | Type | Description |
|---|
| 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. |
| models | set | A 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
| Name | Type | Description |
|---|
| fixture_labels | list | The 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
| Name | Type | Description |
|---|
| obj | [DeserializedObject](../../../serializers/base/deserializedobject.md?sid=django_core_serializers_base_deserializedobject) | The deserialized model instance ready to be persisted to the database. |
Returns
| Type | Description |
|---|
bool | True 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
| Name | Type | Description |
|---|
| fixture_label | str | The 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
| Name | Type | Description |
|---|
| fixture_name | str | The name or path of the fixture to resolve. |
Returns
| Type | Description |
|---|
tuple | A 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
| Name | Type | Description |
|---|
| fixture_name | str | The base name of the fixture file. |
| ser_fmt | str | The specific serialization format to target, or None to search all formats. |
| cmp_fmt | str | The specific compression format to target, or None to search all formats. |
Returns
| Type | Description |
|---|
set | A 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
| Name | Type | Description |
|---|
| fixture_dir | str | The directory path to search within. |
| fixture_name | str | The base name of the fixture being sought. |
| targets | set | The set of valid filenames (including extensions) to look for. |
Returns
| Type | Description |
|---|
list | A 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
| Name | Type | Description |
|---|
| fixture_label | str | The label used to identify and locate fixture files across search directories. |
Returns
| Type | Description |
|---|
list | A 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
| Type | Description |
|---|
list | A 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
| Name | Type | Description |
|---|
| fixture_name | str | The raw filename or label to be decomposed into its constituent parts. |
Returns
| Type | Description |
|---|
tuple | A tuple of (name, serialization_format, compression_format) extracted from the input string. |