diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-11-22 20:09:40 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-11-22 20:53:59 +0100 |
| commit | a026e480da89cb99c9dc6c954fb5a37ded0f9315 (patch) | |
| tree | 5203483ad859b6849a2b7785281c62e007cd9317 /django | |
| parent | ea6b95dbec77371d517392ffb465017b8eb7001c (diff) | |
Fixed #16039 -- Made post_syncdb handlers multi-db aware.
Also reverted 8fb7a9002669fb7ba7bec7df90b465b92e1ed3c2. Refs #17055.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/auth/management/__init__.py | 20 | ||||
| -rw-r--r-- | django/contrib/contenttypes/management.py | 14 |
2 files changed, 23 insertions, 11 deletions
diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py index b5fd29a1c2..ce5d57fa79 100644 --- a/django/contrib/auth/management/__init__.py +++ b/django/contrib/auth/management/__init__.py @@ -10,6 +10,7 @@ import unicodedata from django.contrib.auth import models as auth_app, get_user_model from django.core import exceptions from django.core.management.base import CommandError +from django.db import DEFAULT_DB_ALIAS, router from django.db.models import get_models, signals from django.utils import six from django.utils.six.moves import input @@ -57,7 +58,10 @@ def _check_permission_clashing(custom, builtin, ctype): (codename, ctype.app_label, ctype.model_class().__name__)) pool.add(codename) -def create_permissions(app, created_models, verbosity, **kwargs): +def create_permissions(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, **kwargs): + if not router.allow_syncdb(db, auth_app.Permission): + return + from django.contrib.contenttypes.models import ContentType app_models = get_models(app) @@ -68,7 +72,9 @@ def create_permissions(app, created_models, verbosity, **kwargs): # The codenames and ctypes that should exist. ctypes = set() for klass in app_models: - ctype = ContentType.objects.get_for_model(klass) + # Force looking up the content types in the current database + # before creating foreign keys to them. + ctype = ContentType.objects.db_manager(db).get_for_model(klass) ctypes.add(ctype) for perm in _get_all_permissions(klass._meta, ctype): searched_perms.append((ctype, perm)) @@ -76,21 +82,21 @@ def create_permissions(app, created_models, verbosity, **kwargs): # Find all the Permissions that have a context_type for a model we're # looking for. We don't need to check for codenames since we already have # a list of the ones we're going to create. - all_perms = set(auth_app.Permission.objects.filter( + all_perms = set(auth_app.Permission.objects.using(db).filter( content_type__in=ctypes, ).values_list( "content_type", "codename" )) - objs = [ + perms = [ auth_app.Permission(codename=codename, name=name, content_type=ctype) for ctype, (codename, name) in searched_perms if (ctype.pk, codename) not in all_perms ] - auth_app.Permission.objects.bulk_create(objs) + auth_app.Permission.objects.using(db).bulk_create(perms) if verbosity >= 2: - for obj in objs: - print("Adding permission '%s'" % obj) + for perm in perms: + print("Adding permission '%s'" % perm) def create_superuser(app, created_models, verbosity, db, **kwargs): diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py index 9f287d494b..85f76bac77 100644 --- a/django/contrib/contenttypes/management.py +++ b/django/contrib/contenttypes/management.py @@ -1,14 +1,18 @@ from django.contrib.contenttypes.models import ContentType +from django.db import DEFAULT_DB_ALIAS, router from django.db.models import get_apps, get_models, signals from django.utils.encoding import smart_text from django.utils import six from django.utils.six.moves import input -def update_contenttypes(app, created_models, verbosity=2, **kwargs): +def update_contenttypes(app, created_models, verbosity=2, db=DEFAULT_DB_ALIAS, **kwargs): """ Creates content types for models in the given app, removing any model entries that no longer have a matching model class. """ + if not router.allow_syncdb(db, ContentType): + return + ContentType.objects.clear_cache() app_models = get_models(app) if not app_models: @@ -19,10 +23,11 @@ def update_contenttypes(app, created_models, verbosity=2, **kwargs): (model._meta.object_name.lower(), model) for model in app_models ) + # Get all the content types content_types = dict( (ct.model, ct) - for ct in ContentType.objects.filter(app_label=app_label) + for ct in ContentType.objects.using(db).filter(app_label=app_label) ) to_remove = [ ct @@ -30,7 +35,7 @@ def update_contenttypes(app, created_models, verbosity=2, **kwargs): if model_name not in app_models ] - cts = ContentType.objects.bulk_create([ + cts = [ ContentType( name=smart_text(model._meta.verbose_name_raw), app_label=app_label, @@ -38,7 +43,8 @@ def update_contenttypes(app, created_models, verbosity=2, **kwargs): ) for (model_name, model) in six.iteritems(app_models) if model_name not in content_types - ]) + ] + ContentType.objects.using(db).bulk_create(cts) if verbosity >= 2: for ct in cts: print("Adding content type '%s | %s'" % (ct.app_label, ct.model)) |
