diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 07160217..5fdc3150 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -20,3 +20,4 @@ * [naquiroz](https://github.com/naquiroz) * [john-sandall](https://github.com/john-sandall) * [dineshtrivedi](https://github.com/dineshtrivedi) +* [TheAfroOfDoom](https://github.com/TheAfroOfDoom) diff --git a/pylint_django/checkers/foreign_key_strings.py b/pylint_django/checkers/foreign_key_strings.py index c145301e..477c4958 100644 --- a/pylint_django/checkers/foreign_key_strings.py +++ b/pylint_django/checkers/foreign_key_strings.py @@ -1,6 +1,9 @@ from __future__ import absolute_import +import os + import astroid +from configurations import importer from pylint.checkers import BaseChecker from pylint.checkers.utils import check_messages from pylint.interfaces import IAstroidChecker @@ -41,6 +44,15 @@ class ForeignKeyStringsChecker(BaseChecker): "help": "A module containing Django settings to be used while linting.", }, ), + ( + "django-configuration", + { + "default": None, + "type": "string", + "metavar": "", + "help": "The configuration for Django to use while linting.", + }, + ), ) msgs = { @@ -89,6 +101,25 @@ def open(self): try: import django # pylint: disable=import-outside-toplevel + if os.environ.get("DJANGO_SETTINGS_MODULE") is None or os.environ.get("DJANGO_CONFIGURATION") is None: + try: + os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", + os.environ.get("DJANGO_SETTINGS_MODULE") or self.config.django_settings_module, + ) + os.environ.setdefault( + "DJANGO_CONFIGURATION", + os.environ.get("DJANGO_CONFIGURATION") or self.config.django_configuration, + ) + except TypeError as ex: + missing_module = "" + if self.config.django_settings_module is None: + missing_module = "DJANGO_SETTINGS_MODULE" + else: + missing_module = "DJANGO_CONFIGURATION" + raise RuntimeError(f"{missing_module} required to initialize Django project settings") from ex + importer.install() + django.setup() from django.apps import ( # noqa pylint: disable=import-outside-toplevel,unused-import apps,