{"id":3230,"library":"pylint-django","title":"Pylint Django Plugin","description":"Pylint-Django is a Pylint plugin designed to help Pylint understand the intricacies of the Django web framework. It provides specific checkers for Django models, forms, admin, and other components, catching common Django-related issues that a standard Pylint run would miss. The current version is 2.7.0, and it generally releases new versions to support new major releases of Pylint or Django, typically a few times a year.","status":"active","version":"2.7.0","language":"en","source_language":"en","source_url":"https://github.com/pylint-dev/pylint-django","tags":["pylint","django","linting","code quality","plugin"],"install":[{"cmd":"pip install pylint-django pylint django","lang":"bash","label":"Install Pylint-Django, Pylint, and Django"}],"dependencies":[{"reason":"Internal utility package required by pylint-django.","package":"pylint-django-utils","optional":false},{"reason":"Pylint is the core linter that this package extends. Must be installed separately.","package":"pylint","optional":true},{"reason":"Django is the framework this plugin analyzes. Must be installed separately.","package":"django","optional":true}],"imports":[{"note":"Pylint-Django is a Pylint plugin loaded via the Pylint CLI `--load-plugins` argument or configuration file (`load-plugins=pylint_django`), not via a direct Python `import` statement in user code.","symbol":"pylint_django","correct":"pylint --load-plugins pylint_django <your_django_app_path>"}],"quickstart":{"code":"import os\nimport subprocess\nimport tempfile\nimport shutil\n\n# Create a temporary directory to simulate a Django app structure\nwith tempfile.TemporaryDirectory() as temp_dir:\n    app_dir = os.path.join(temp_dir, \"myapp\")\n    os.makedirs(app_dir)\n\n    # Create a dummy models.py that pylint-django can analyze\n    models_code = '''\nfrom django.db import models\n\nclass Author(models.Model):\n    name = models.CharField(max_length=100)\n\nclass Book(models.Model):\n    title = models.CharField(max_length=100)\n    # pylint-django will check ForeignKey attributes, e.g., missing related_name\n    author = models.ForeignKey(Author, on_delete=models.CASCADE)\n\n    class Meta:\n        ordering = ['title']\n\n    def __str__(self):\n        return self.title\n'''\n    models_file_path = os.path.join(app_dir, \"models.py\")\n    with open(models_file_path, \"w\") as f:\n        f.write(models_code)\n\n    print(f\"Created temporary Django app file at: {models_file_path}\")\n\n    # Create a minimal settings.py required for Django's ORM and app loading\n    settings_module_path = os.path.join(temp_dir, \"settings.py\")\n    with open(settings_module_path, \"w\") as f:\n        f.write('''\nINSTALLED_APPS = ['myapp']\nSECRET_KEY = 'insecure-key-for-testing'\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.sqlite3',\n        'NAME': 'db.sqlite3',\n    }\n}\n''')\n    # Set DJANGO_SETTINGS_MODULE environment variable for Pylint's context\n    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'\n\n    print(\"Running Pylint with pylint-django loaded...\")\n    try:\n        # Execute pylint from the temporary directory to correctly pick up settings.py\n        result = subprocess.run(\n            [\n                \"pylint\",\n                \"--load-plugins\",\n                \"pylint_django\",\n                \"--disable=all\", # Disable default Pylint checks for clearer output\n                \"--enable=django-model-checks,django-form-checks,django-admin-checks,django-template-checks,django-other-checks\", # Enable specific pylint-django checkers\n                \"--rcfile=NONE\", # Ignore user's pylintrc for consistent results\n                models_file_path # Target file for linting\n            ],\n            capture_output=True,\n            text=True,\n            check=False, # Do not raise exception for non-zero exit codes (linting errors/warnings)\n            cwd=temp_dir # Run subprocess in the temporary directory\n        )\n        print(\"\\n--- Pylint Output ---\")\n        print(result.stdout)\n        if result.stderr:\n            print(\"\\n--- Pylint Errors (stderr) ---\")\n            print(result.stderr)\n        print(\"--- End Pylint Output ---\")\n    except FileNotFoundError:\n        print(\"Error: 'pylint' command not found. Please ensure Pylint and pylint-django are installed.\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")\n    finally:\n        # Clean up the environment variable\n        del os.environ['DJANGO_SETTINGS_MODULE']\n","lang":"python","description":"This quickstart demonstrates how to run `pylint` with the `pylint-django` plugin enabled on a mock Django `models.py` file. It sets up a minimal environment to allow Django's ORM to be initialized, ensuring `pylint-django` can perform its checks. The output will include any Django-specific linting issues identified by the plugin."},"warnings":[{"fix":"Upgrade to Python 3.9+ and ensure Pylint is version 3.0.0 or higher. For example: `pip install 'pylint>=3.0.0' 'python_version>=3.9'`.","message":"Pylint-Django v2.6.1 and newer dropped support for Python 3.7 and 3.8. Additionally, it requires Pylint 3.0 or higher.","severity":"breaking","affected_versions":">=2.6.1"},{"fix":"If using Pylint 4.0.0 or later, ensure `pylint-django` is upgraded to v2.7.0 or higher: `pip install --upgrade pylint-django`.","message":"Pylint-Django v2.7.0 added support for Pylint 4.0.0+. Older versions of `pylint-django` (before 2.7.0) are not compatible with Pylint 4.x and will likely cause crashes or incorrect analysis.","severity":"breaking","affected_versions":"<2.7.0"},{"fix":"Always include `--load-plugins pylint_django` when invoking `pylint` or ensure it's configured in your Pylint configuration file.","message":"The `pylint-django` plugin must be explicitly loaded by Pylint using the `--load-plugins pylint_django` CLI argument or by adding `load-plugins=pylint_django` to your `.pylintrc` or `pyproject.toml` configuration.","severity":"gotcha","affected_versions":"all"},{"fix":"Before running Pylint, set `export DJANGO_SETTINGS_MODULE='your_project.settings'` (or `set DJANGO_SETTINGS_MODULE=your_project.settings` on Windows) in your shell or CI script.","message":"When running Pylint outside of a Django project's `manage.py` command (e.g., directly on files or within a CI pipeline), you must set the `DJANGO_SETTINGS_MODULE` environment variable to a valid Django settings file for `pylint-django` to properly analyze models and other Django components.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}