本文整理汇总了Python中django.core.serializers.sort_dependencies方法的典型用法代码示例。如果您正苦于以下问题:Python serializers.sort_dependencies方法的具体用法?Python serializers.sort_dependencies怎么用?Python serializers.sort_dependencies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.serializers
的用法示例。
在下文中一共展示了serializers.sort_dependencies方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: serialize_db_to_string
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def serialize_db_to_string(self):
"""
Serializes all data in the database into a JSON string.
Designed only for test runner usage; will not handle large
amounts of data.
"""
# Build list of all apps to serialize
from django.db.migrations.loader import MigrationLoader
loader = MigrationLoader(self.connection)
app_list = []
for app_config in apps.get_app_configs():
if (
app_config.models_module is not None and
app_config.label in loader.migrated_apps and
app_config.name not in settings.TEST_NON_SERIALIZED_APPS
):
app_list.append((app_config, None))
# Make a function to iteratively return every object
def get_objects():
for model in serializers.sort_dependencies(app_list):
if (not model._meta.proxy and model._meta.managed and
router.allow_migrate_model(self.connection.alias, model)):
queryset = model._default_manager.using(self.connection.alias).order_by(model._meta.pk.name)
for obj in queryset.iterator():
yield obj
# Serialize to a string
out = StringIO()
serializers.serialize("json", get_objects(), indent=None, stream=out)
return out.getvalue()
示例2: serialize_db_to_string
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def serialize_db_to_string(self):
"""
Serialize all data in the database into a JSON string.
Designed only for test runner usage; will not handle large
amounts of data.
"""
# Build list of all apps to serialize
from django.db.migrations.loader import MigrationLoader
loader = MigrationLoader(self.connection)
app_list = []
for app_config in apps.get_app_configs():
if (
app_config.models_module is not None and
app_config.label in loader.migrated_apps and
app_config.name not in settings.TEST_NON_SERIALIZED_APPS
):
app_list.append((app_config, None))
# Make a function to iteratively return every object
def get_objects():
for model in serializers.sort_dependencies(app_list):
if (model._meta.can_migrate(self.connection) and
router.allow_migrate_model(self.connection.alias, model)):
queryset = model._default_manager.using(self.connection.alias).order_by(model._meta.pk.name)
yield from queryset.iterator()
# Serialize to a string
out = StringIO()
serializers.serialize("json", get_objects(), indent=None, stream=out)
return out.getvalue()
示例3: serialize_db_to_string
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def serialize_db_to_string(self):
"""
Serializes all data in the database into a JSON string.
Designed only for test runner usage; will not handle large
amounts of data.
"""
# Build list of all apps to serialize
from django.db.migrations.loader import MigrationLoader
loader = MigrationLoader(self.connection)
app_list = []
for app_config in apps.get_app_configs():
if (
app_config.models_module is not None and
app_config.label in loader.migrated_apps and
app_config.name not in settings.TEST_NON_SERIALIZED_APPS
):
app_list.append((app_config, None))
# Make a function to iteratively return every object
def get_objects():
for model in serializers.sort_dependencies(app_list):
if (model._meta.can_migrate(self.connection) and
router.allow_migrate_model(self.connection.alias, model)):
queryset = model._default_manager.using(self.connection.alias).order_by(model._meta.pk.name)
for obj in queryset.iterator():
yield obj
# Serialize to a string
out = StringIO()
serializers.serialize("json", get_objects(), indent=None, stream=out)
return out.getvalue()
示例4: test_dependency_sorting
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting(self):
"""
It doesn't matter what order you mention the models, Store *must* be
serialized before then Person, and both must be serialized before Book.
"""
sorted_deps = serializers.sort_dependencies(
[('fixtures_regress', [Book, Person, Store])]
)
self.assertEqual(
sorted_deps,
[Store, Person, Book]
)
示例5: test_dependency_sorting_2
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_2(self):
sorted_deps = serializers.sort_dependencies(
[('fixtures_regress', [Book, Store, Person])]
)
self.assertEqual(
sorted_deps,
[Store, Person, Book]
)
示例6: test_dependency_sorting_3
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_3(self):
sorted_deps = serializers.sort_dependencies(
[('fixtures_regress', [Store, Book, Person])]
)
self.assertEqual(
sorted_deps,
[Store, Person, Book]
)
示例7: test_dependency_sorting_4
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_4(self):
sorted_deps = serializers.sort_dependencies(
[('fixtures_regress', [Store, Person, Book])]
)
self.assertEqual(
sorted_deps,
[Store, Person, Book]
)
示例8: test_dependency_sorting_5
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_5(self):
sorted_deps = serializers.sort_dependencies(
[('fixtures_regress', [Person, Book, Store])]
)
self.assertEqual(
sorted_deps,
[Store, Person, Book]
)
示例9: test_dependency_sorting_dangling
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_dangling(self):
sorted_deps = serializers.sort_dependencies(
[('fixtures_regress', [Person, Circle1, Store, Book])]
)
self.assertEqual(
sorted_deps,
[Circle1, Store, Person, Book]
)
示例10: test_dependency_sorting_tight_circular
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_tight_circular(self):
with self.assertRaisesMessage(
RuntimeError,
"Can't resolve dependencies for fixtures_regress.Circle1, "
"fixtures_regress.Circle2 in serialized app list."
):
serializers.sort_dependencies([('fixtures_regress', [Person, Circle2, Circle1, Store, Book])])
示例11: test_dependency_sorting_tight_circular_2
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_tight_circular_2(self):
with self.assertRaisesMessage(
RuntimeError,
"Can't resolve dependencies for fixtures_regress.Circle1, "
"fixtures_regress.Circle2 in serialized app list."
):
serializers.sort_dependencies([('fixtures_regress', [Circle1, Book, Circle2])])
示例12: test_dependency_self_referential
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_self_referential(self):
with self.assertRaisesMessage(
RuntimeError,
"Can't resolve dependencies for fixtures_regress.Circle3 in "
"serialized app list."
):
serializers.sort_dependencies([('fixtures_regress', [Book, Circle3])])
示例13: test_dependency_sorting_long
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_long(self):
with self.assertRaisesMessage(
RuntimeError,
"Can't resolve dependencies for fixtures_regress.Circle1, "
"fixtures_regress.Circle2, fixtures_regress.Circle3 in serialized "
"app list."
):
serializers.sort_dependencies([('fixtures_regress', [Person, Circle2, Circle1, Circle3, Store, Book])])
示例14: test_dependency_sorting_m2m_simple
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_m2m_simple(self):
"""
M2M relations without explicit through models SHOULD count as dependencies
Regression test for bugs that could be caused by flawed fixes to
#14226, namely if M2M checks are removed from sort_dependencies
altogether.
"""
sorted_deps = serializers.sort_dependencies(
[('fixtures_regress', [M2MSimpleA, M2MSimpleB])]
)
self.assertEqual(sorted_deps, [M2MSimpleB, M2MSimpleA])
示例15: test_dependency_sorting_m2m_simple_circular
# 需要导入模块: from django.core import serializers [as 别名]
# 或者: from django.core.serializers import sort_dependencies [as 别名]
def test_dependency_sorting_m2m_simple_circular(self):
"""
Resolving circular M2M relations without explicit through models should
fail loudly
"""
with self.assertRaisesMessage(
RuntimeError,
"Can't resolve dependencies for fixtures_regress.M2MSimpleCircularA, "
"fixtures_regress.M2MSimpleCircularB in serialized app list."
):
serializers.sort_dependencies([('fixtures_regress', [M2MSimpleCircularA, M2MSimpleCircularB])])