{"id":8992,"library":"flake8-django","title":"flake8-django","description":"flake8-django is a plugin for the Flake8 static code analysis tool, specifically designed to detect bad practices and enforce style guidelines within Django projects. Currently at version 1.4, it sees active, though irregular, development with its last major release in July 2023.","status":"active","version":"1.4","language":"en","source_language":"en","source_url":"https://github.com/rocioar/flake8-django","tags":["flake8","django","linter","code quality","python"],"install":[{"cmd":"pip install flake8-django flake8 Django","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"flake8-django is a plugin for Flake8 and requires it to run. It is a peer dependency.","package":"flake8","optional":false},{"reason":"The plugin analyzes Django-specific code and requires Django to be present in the environment for meaningful checks. It is a peer dependency.","package":"Django","optional":false},{"reason":"Requires Python version 3.7.2 or later, but less than 4.0.0.","package":"python","version":">=3.7.2,<4.0.0","optional":false}],"imports":[{"note":"flake8-django is a plugin, not a library meant for direct import and use in Python code. Its checks are integrated when running the `flake8` command.","symbol":"flake8_django","correct":"No direct import needed. flake8 automatically discovers installed plugins."}],"quickstart":{"code":"import os\n\n# Example Django model for demonstration\n# This code would typically be in a models.py file\nfrom django.db import models\n\nclass MyModel(models.Model):\n    # DJ01: Avoid using null=True on string-based fields\n    name = models.CharField(max_length=255, null=True, blank=True) # Will trigger DJ01\n    description = models.TextField(default='Default description')\n\n    class Meta:\n        # DJ08: Model does not define __str__ method (implicit)\n        pass\n\n# To run flake8-django, save the above in a file (e.g., myapp/models.py)\n# Then, from your project root in the terminal:\n# pip install flake8 flake8-django\n# flake8 .\n# If specific rules like DJ10/DJ11 are desired:\n# flake8 --select=E,F,W,C90,DJ,DJ10,DJ11 .\n","lang":"python","description":"Install flake8 and flake8-django. Then, run `flake8 .` from your Django project's root directory. flake8-django's checks (prefixed with 'DJ') will be automatically included in the output. Some rules, like DJ10 and DJ11 (verbose_name checks), are optional and must be explicitly enabled via the `--select` argument or in a `.flake8` configuration file."},"warnings":[{"fix":"Upgrade to Python 3.7.2 or higher, or pin flake8-django to `<1.1.5`.","message":"Version 1.1.5 dropped support for Python versions below 3.7. Projects still on older Python versions must use an earlier flake8-django release.","severity":"breaking","affected_versions":"<1.1.5"},{"fix":"Ensure your `flake8-django` version is compatible with your `flake8` installation. Consult the GitHub release notes or PyPI for specific compatibility ranges.","message":"flake8-django versions require specific flake8 versions for compatibility. v1.1.3 added compatibility for flake8 4.x, and v1.1.5 added compatibility for flake8 5.x. Users might encounter issues if using incompatible versions.","severity":"gotcha","affected_versions":"All versions, depending on flake8 compatibility"},{"fix":"Enable optional rules by adding them to the `--select` argument when running `flake8` (e.g., `flake8 --select=...,DJ10,DJ11 .`) or by configuring them in your `.flake8` or `pyproject.toml` file.","message":"Optional rules (e.g., DJ10 for `verbose_name`, DJ11 for `verbose_name_plural`) are disabled by default and will not be reported unless explicitly selected.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For optional string fields, use `blank=True` and set a `default` value or handle `null` explicitly, as empty strings are usually preferred over `NULL` in databases for string fields. E.g., `name = models.CharField(max_length=255, blank=True, default='')`.","message":"The DJ01 check warns against using `null=True` on string-based model fields like `CharField` and `TextField`, which is a common Django anti-pattern.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `flake8` using pip: `pip install flake8`. Ensure your virtual environment is activated if applicable.","cause":"The `flake8` package is not installed or not available in the system's PATH.","error":"flake8: command not found"},{"fix":"Install `flake8-django`: `pip install flake8-django`. If in a virtual environment, ensure it's active.","cause":"The `flake8-django` package is not installed in the current Python environment where `flake8` is being run, or there's an environment issue.","error":"Failed to load plugin 'flake8_django' (ModuleNotFoundError: No module named 'flake8_django')"},{"fix":"Add `from __future__ import annotations` at the top of your Python file. For Django models referencing other models or managers, ensure all related types are properly imported or handled with forward references.","cause":"This error, though a standard PyFlakes (F8xx) error, can frequently occur in Django projects when using type annotations, especially with forward references in models or managers, if `from __future__ import annotations` is not used or configured incorrectly.","error":"F821 undefined name '...' (often related to Django Models and Managers)"},{"fix":"Configure `flake8` to allow a longer line length by creating a `.flake8` file in your project root or adding to `pyproject.toml`: `[flake8]\nmax-line-length = 88` (or your preferred length).","cause":"While not directly from `flake8-django`, this is a common `pycodestyle` (part of `flake8`) error that users encounter in Django projects. Django's official style guide, and tools like Black, often use a longer line length (e.g., 88 characters) than PEP 8's default 79/80, causing conflicts.","error":"E501 line too long (80 > 79)"}]}