From 563cc887285a739b6e1f3743136b296611ce9206 Mon Sep 17 00:00:00 2001 From: Branko Majic Date: Tue, 27 Apr 2021 23:16:18 +0200 Subject: [PATCH] Fix django.contrib.sites detection in set_default_site command when using AppConfig in INSTALLED_APPS: django.contrib.sites application can be specified in installed application list either via its module path, or via its AppConfig path. This can be either the default one (django.contrib.sites.apps.SitesConfig), or user-provided AppConfig class. Instead of relying on a fixed name in the settings files, use the application registry to check if the sites application is installed. --- django_extensions/management/commands/set_default_site.py | 3 ++- tests/management/commands/test_set_default_site.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/django_extensions/management/commands/set_default_site.py b/django_extensions/management/commands/set_default_site.py index 295d0aad6..1ea5fc273 100644 --- a/django_extensions/management/commands/set_default_site.py +++ b/django_extensions/management/commands/set_default_site.py @@ -3,6 +3,7 @@ from django.conf import settings from django.core.management.base import BaseCommand, CommandError +from django.apps import apps from django_extensions.management.utils import signalcommand @@ -29,7 +30,7 @@ def add_arguments(self, parser): @signalcommand def handle(self, *args, **options): - if 'django.contrib.sites' not in settings.INSTALLED_APPS: + if not apps.is_installed('django.contrib.sites'): raise CommandError('The sites framework is not installed.') from django.contrib.sites.models import Site diff --git a/tests/management/commands/test_set_default_site.py b/tests/management/commands/test_set_default_site.py index 5d4950b07..eb596d6d2 100644 --- a/tests/management/commands/test_set_default_site.py +++ b/tests/management/commands/test_set_default_site.py @@ -82,3 +82,10 @@ def test_should_set_domain_only(self): self.assertEqual(result.name, 'example.com') self.assertEqual(result.domain, 'bar') + + def test_should_not_raise_if_sites_installed_through_appconfig(self): + with self.modify_settings(INSTALLED_APPS={ + 'append': 'django.contrib.sites.apps.SitesConfig', + 'remove': 'django.contrib.sites', + }): + call_command('set_default_site', '--name=foo', '--domain=foo.bar')