Skip to main content

sort_dependencies

Sort a list of (app_config, models) pairs into a single list of models.

The single list of models is sorted so that any model with a natural key is serialized before a normal model, and any model with a natural key dependency has it's dependencies serialized first.

If allow_cycles is True, return the best-effort ordering that will respect most of dependencies but ignore some of them to break the cycles.

def sort_dependencies(
app_list: list,
allow_cycles: boolean = False
) - > list

Sort a list of (app_config, models) pairs into a single list of models. The single list of models is sorted so that any model with a natural key is serialized before a normal model, and any model with a natural key dependency has it's dependencies serialized first.

Parameters

NameTypeDescription
app_listlistA list of tuples containing an AppConfig instance and an optional iterable of model classes to be processed.
allow_cyclesboolean = FalseIf True, return the best-effort ordering that will respect most of dependencies but ignore some of them to break the cycles.

Returns

TypeDescription
listA flat list of Django model classes ordered according to their natural key and foreign key dependencies.