本文整理汇总了Python中django.apps.apps方法的典型用法代码示例。如果您正苦于以下问题:Python apps.apps方法的具体用法?Python apps.apps怎么用?Python apps.apps使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.apps
的用法示例。
在下文中一共展示了apps.apps方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_permissions
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def update_permissions(sender, app_config, verbosity, apps=global_apps,
**kwargs):
settings_models = getattr(settings, 'ADMIN_VIEW_PERMISSION_MODELS', None)
# TODO: Maybe look at the registry not in all models
for app in apps.get_app_configs():
for model in app.get_models():
view_permission = 'view_%s' % model._meta.model_name
if settings_models or (settings_models is not None and len(
settings_models) == 0):
model_name = get_model_name(model)
if model_name in settings_models and view_permission not in \
[perm[0] for perm in model._meta.permissions]:
model._meta.permissions += (
(view_permission,
'Can view %s' % model._meta.model_name),)
else:
if view_permission not in [perm[0] for perm in
model._meta.permissions]:
model._meta.permissions += (
('view_%s' % model._meta.model_name,
'Can view %s' % model._meta.model_name),)
示例2: handle
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def handle(self, *args, **options):
changed = set()
self.stdout.write("Checking...")
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError:
sys.exit("Unable to check migrations: cannot connect to database\n")
autodetector = MigrationAutodetector(
executor.loader.project_state(), ProjectState.from_apps(apps),
)
changed.update(autodetector.changes(graph=executor.loader.graph).keys())
changed -= set(options["ignore"])
if changed:
sys.exit(
"Apps with model changes but no corresponding migration file: %(changed)s\n"
% {"changed": list(changed)}
)
else:
sys.stdout.write("All migration files present\n")
示例3: test_migration_11
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_migration_11(self):
# create users with the CI feature off
# to replicate pre feature database state
with override_settings(ST_CASE_INSENSITIVE_USERNAMES=False):
utils.create_user(username='FOO')
utils.create_user(username='BaR')
utils.create_user(username='baz')
# default all nicknames to empty
self.assertEqual(
UserProfile.objects.all().update(nickname=''), 3)
data_migration_11.populate_nickname(apps, None)
self.assertEqual(
[u.nickname for u in UserProfile.objects.all()],
['FOO', 'BaR', 'baz'])
self.assertEqual(
[u.username for u in User.objects.all()],
['FOO', 'BaR', 'baz'])
data_migration_11.make_usernames_lower(apps, None)
self.assertEqual(
[u.username for u in User.objects.all()],
['foo', 'bar', 'baz'])
self.assertEqual(
[u.nickname for u in UserProfile.objects.all()],
['FOO', 'BaR', 'baz'])
示例4: test_migration_11_no_ci_usernames
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_migration_11_no_ci_usernames(self):
utils.create_user(username='FOO')
utils.create_user(username='foo')
utils.create_user(username='BaR')
utils.create_user(username='bar')
utils.create_user(username='baz')
self.assertEqual(
UserProfile.objects.all().update(nickname=''), 5)
data_migration_11.populate_nickname(apps, None)
self.assertEqual(
[u.nickname for u in UserProfile.objects.all()],
['FOO', 'foo', 'BaR', 'bar', 'baz'])
self.assertEqual(
[u.username for u in User.objects.all()],
['FOO', 'foo', 'BaR', 'bar', 'baz'])
data_migration_11.make_usernames_lower(apps, None)
self.assertEqual(
[u.username for u in User.objects.all()],
['FOO', 'foo', 'BaR', 'bar', 'baz'])
self.assertEqual(
[u.nickname for u in UserProfile.objects.all()],
['FOO', 'foo', 'BaR', 'bar', 'baz'])
示例5: test_migration_11_make_usernames_lower_integrity_err
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_migration_11_make_usernames_lower_integrity_err(self):
with override_settings(ST_CASE_INSENSITIVE_USERNAMES=False):
utils.create_user(username='FOO')
utils.create_user(username='fOo')
utils.create_user(username='Foo')
utils.create_user(username='bar')
utils.create_user(username='bAr')
utils.create_user(username='baz')
self.assertEqual(
[u.username for u in User.objects.all()],
['FOO', 'fOo', 'Foo', 'bar', 'bAr', 'baz'])
# transaction is already handled
with self.assertRaises(IntegrityError) as cm:
data_migration_11.make_usernames_lower(apps, None)
self.maxDiff = None
self.assertEqual(
str(cm.exception),
"There are two or more users with similar name but "
"different casing, for example: someUser and SomeUser, "
"either remove one of them or set the "
"`ST_CASE_INSENSITIVE_USERNAMES` setting to False. "
"Then run the upgrade/migration again. Any change was reverted. "
"Duplicate users are ['FOO', 'fOo', 'Foo', 'bar', 'bAr']")
示例6: test_migration_11_idempotency
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_migration_11_idempotency(self):
"""Should be idempotent"""
with override_settings(ST_CASE_INSENSITIVE_USERNAMES=False):
utils.create_user(username='FOO')
self.assertEqual(
UserProfile.objects.all().update(nickname=''), 1)
data_migration_11.populate_nickname(apps, None)
data_migration_11.make_usernames_lower(apps, None)
self.assertEqual(
[u.username for u in User.objects.all()],
['foo'])
self.assertEqual(
[u.nickname for u in UserProfile.objects.all()],
['FOO'])
data_migration_11.populate_nickname(apps, None)
data_migration_11.make_usernames_lower(apps, None)
self.assertEqual(
[u.username for u in User.objects.all()],
['foo'])
self.assertEqual(
[u.nickname for u in UserProfile.objects.all()],
['FOO'])
示例7: test_abstract_model_pending_operations
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_abstract_model_pending_operations(self):
"""
Many-to-many fields declared on abstract models should not add lazy
relations to resolve relationship declared as string (#24215).
"""
pending_ops_before = list(apps._pending_operations.items())
class AbstractManyToManyModel(models.Model):
fk = models.ForeignKey('missing.FK', models.CASCADE)
class Meta:
abstract = True
self.assertIs(AbstractManyToManyModel._meta.apps, apps)
self.assertEqual(
pending_ops_before,
list(apps._pending_operations.items()),
'Pending lookup added for a many-to-many field on an abstract model'
)
示例8: test_abstract_model_pending_operations
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_abstract_model_pending_operations(self):
"""
Foreign key fields declared on abstract models should not add lazy
relations to resolve relationship declared as string (#24215).
"""
pending_ops_before = list(apps._pending_operations.items())
class AbstractForeignKeyModel(models.Model):
fk = models.ForeignKey('missing.FK', models.CASCADE)
class Meta:
abstract = True
self.assertIs(AbstractForeignKeyModel._meta.apps, apps)
self.assertEqual(
pending_ops_before,
list(apps._pending_operations.items()),
'Pending lookup added for a foreign key on an abstract model'
)
示例9: add_field
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def add_field(field, filters: List[str]):
"""Adds the specified field to a model.
Arguments:
field:
The field to add to a model.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model()
state = migrations.state.ProjectState.from_apps(apps)
apply_migration([migrations.CreateModel(model.__name__, fields=[])], state)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.AddField(model.__name__, "title", field)], state
)
yield calls
示例10: run
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def run(self, *args, **kwargs):
changed = set()
log.info("Checking DB migrations")
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError:
log.critical("Unable to check migrations, cannot connect to database")
sys.exit(1)
autodetector = MigrationAutodetector(
executor.loader.project_state(), ProjectState.from_apps(apps)
)
changed.update(autodetector.changes(graph=executor.loader.graph).keys())
if changed:
log.critical(
"Apps with model changes but no corresponding "
f"migration file: {list(changed)}"
)
sys.exit(1)
else:
log.info("All migration files present")
示例11: add_arguments
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def add_arguments(self, parser):
parser.add_argument(
"--ignore",
action="store",
nargs="+",
dest="ignore",
default=[],
help="Comma separated list of apps to ignore missing migration files. "
"Useful for specifying third-party ones here.",
)
示例12: test_for_missing_migrations
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_for_missing_migrations(self):
"""Checks if there're models changes which aren't reflected in migrations."""
migrations_loader = MigrationExecutor(connection).loader
migrations_detector = MigrationAutodetector(
from_state=migrations_loader.project_state(),
to_state=ProjectState.from_apps(apps)
)
if migrations_detector.changes(graph=migrations_loader.graph):
self.fail(
'Your models have changes that are not yet reflected '
'in a migration. You should add them now.'
)
示例13: test_for_missing_migrations
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_for_missing_migrations(self):
"""Checks if there're models changes which aren't reflected in migrations."""
migrations_loader = MigrationExecutor(connection).loader
migrations_detector = MigrationAutodetector(
from_state=migrations_loader.project_state(),
to_state=ProjectState.from_apps(apps),
)
if migrations_detector.changes(graph=migrations_loader.graph):
self.fail(
"Your models have changes that are not yet reflected "
"in a migration. You should add them now."
)
示例14: handle
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def handle(self, *args, **kwargs):
changed = set()
ignore_list = ['authtools'] # dependencies that we don't care about migrations for (usually for testing only)
self.stdout.write("Checking...")
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError:
sys.exit("Unable to check migrations: cannot connect to database\n")
autodetector = MigrationAutodetector(
executor.loader.project_state(),
ProjectState.from_apps(apps),
)
changed.update(autodetector.changes(graph=executor.loader.graph).keys())
for ignore in ignore_list:
if ignore in changed:
changed.remove(ignore)
if changed:
sys.exit("Apps with model changes but no corresponding migration file: %(changed)s\n" % {
'changed': list(changed)
})
else:
sys.stdout.write("All migration files present\n")
示例15: test_migration_profiles
# 需要导入模块: from django import apps [as 别名]
# 或者: from django.apps import apps [as 别名]
def test_migration_profiles(self):
"""Should create profile for existing users"""
foo = utils.create_user(username='foo')
bar = utils.create_user(username='bar')
UserProfile.objects.filter(user=foo).delete()
UserProfile.objects.filter(user=bar).delete()
foo = User.objects.get(pk=foo.pk)
bar = User.objects.get(pk=bar.pk)
with self.assertRaises(ObjectDoesNotExist):
self.assertIsNone(foo.st)
with self.assertRaises(ObjectDoesNotExist):
self.assertIsNone(bar.st)
data_migration_profiles.migrate_profiles(apps, None)
self.assertTrue(User.objects.get(pk=foo.pk).st.is_verified)
self.assertTrue(User.objects.get(pk=bar.pk).st.is_verified)