diff --git a/django/contrib/auth/apps.py b/django/contrib/auth/apps.py index 3a0cb1aea1..bbfcb7dbd1 100644 --- a/django/contrib/auth/apps.py +++ b/django/contrib/auth/apps.py @@ -1,13 +1,17 @@ from django.apps import AppConfig from django.core import checks from django.contrib.auth.checks import check_user_model - +from django.db.models.signals import post_migrate from django.utils.translation import ugettext_lazy as _ +from .management import create_permissions + class AuthConfig(AppConfig): name = 'django.contrib.auth' verbose_name = _("Authentication and Authorization") def ready(self): + post_migrate.connect(create_permissions, + dispatch_uid="django.contrib.auth.management.create_permissions") checks.register(check_user_model, checks.Tags.models) diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py index 18cdcce598..5440e597c1 100644 --- a/django/contrib/auth/management/__init__.py +++ b/django/contrib/auth/management/__init__.py @@ -7,11 +7,10 @@ import getpass import unicodedata from django.apps import apps -from django.contrib.auth import models as auth_app, get_permission_codename +from django.contrib.auth import get_permission_codename 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 signals from django.utils.encoding import DEFAULT_LOCALE_ENCODING from django.utils import six @@ -149,6 +148,9 @@ def get_default_username(check_db=True): :returns: The username, or an empty string if no username can be determined. """ + # This file is used in apps.py, it should not trigger models import. + from django.contrib.auth import models as auth_app + # If the User model has been swapped out, we can't make any assumptions # about the default user name. if auth_app.User._meta.swapped: @@ -177,7 +179,3 @@ def get_default_username(check_db=True): else: return '' return default_username - - -signals.post_migrate.connect(create_permissions, - dispatch_uid="django.contrib.auth.management.create_permissions") diff --git a/django/contrib/contenttypes/apps.py b/django/contrib/contenttypes/apps.py index 6fc8633762..f6c3d430fd 100644 --- a/django/contrib/contenttypes/apps.py +++ b/django/contrib/contenttypes/apps.py @@ -1,12 +1,16 @@ from django.apps import AppConfig from django.contrib.contenttypes.checks import check_generic_foreign_keys from django.core import checks +from django.db.models.signals import post_migrate from django.utils.translation import ugettext_lazy as _ +from .management import update_contenttypes + class ContentTypesConfig(AppConfig): name = 'django.contrib.contenttypes' verbose_name = _("Content Types") def ready(self): + post_migrate.connect(update_contenttypes) checks.register(check_generic_foreign_keys, checks.Tags.models) diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py index f705330137..6da7c49ef5 100644 --- a/django/contrib/contenttypes/management.py +++ b/django/contrib/contenttypes/management.py @@ -1,6 +1,5 @@ from django.apps import apps from django.db import DEFAULT_DB_ALIAS, router -from django.db.models import signals from django.utils.encoding import smart_text from django.utils import six from django.utils.six.moves import input @@ -92,8 +91,5 @@ def update_all_contenttypes(**kwargs): update_contenttypes(app_config, **kwargs) -signals.post_migrate.connect(update_contenttypes) - - if __name__ == "__main__": update_all_contenttypes() diff --git a/django/contrib/sites/apps.py b/django/contrib/sites/apps.py index 91f0936020..c639c3053b 100644 --- a/django/contrib/sites/apps.py +++ b/django/contrib/sites/apps.py @@ -1,8 +1,14 @@ from django.apps import AppConfig +from django.db.models.signals import post_migrate from django.utils.translation import ugettext_lazy as _ +from .management import create_default_site + class SitesConfig(AppConfig): name = 'django.contrib.sites' verbose_name = _("Sites") + + def ready(self): + post_migrate.connect(create_default_site, sender=self) diff --git a/django/contrib/sites/management.py b/django/contrib/sites/management.py index 8353a6f496..2600d04fbd 100644 --- a/django/contrib/sites/management.py +++ b/django/contrib/sites/management.py @@ -5,7 +5,6 @@ Creates the default Site object. from django.apps import apps from django.core.management.color import no_style from django.db import DEFAULT_DB_ALIAS, connections, router -from django.db.models import signals def create_default_site(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_ALIAS, **kwargs): @@ -38,6 +37,3 @@ def create_default_site(app_config, verbosity=2, interactive=True, db=DEFAULT_DB cursor.execute(command) Site.objects.clear_cache() - - -signals.post_migrate.connect(create_default_site, sender=apps.get_app_config('sites')) diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py index 94264b4af6..aa65d1ab1e 100644 --- a/django/contrib/sites/tests.py +++ b/django/contrib/sites/tests.py @@ -1,7 +1,9 @@ from __future__ import unicode_literals +from django.apps import apps from django.conf import settings from django.core.exceptions import ObjectDoesNotExist, ValidationError +from django.db.models.signals import post_migrate from django.http import HttpRequest from django.test import TestCase, modify_settings, override_settings @@ -118,6 +120,15 @@ class SitesFrameworkTests(TestCase): clear_site_cache(Site, instance=self.site) self.assertEqual(models.SITE_CACHE, {}) + def test_create_default_site_signal(self): + """ + Checks that sending ``post_migrate`` creates the default site. + """ + Site.objects.all().delete() + app_config = apps.get_app_config('sites') + post_migrate.send(sender=app_config, app_config=app_config, verbosity=0) + self.assertTrue(Site.objects.exists()) + class MiddlewareTest(TestCase):