flake8-django
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.
Common errors
-
flake8: command not found
cause The `flake8` package is not installed or not available in the system's PATH.fixInstall `flake8` using pip: `pip install flake8`. Ensure your virtual environment is activated if applicable. -
Failed to load plugin 'flake8_django' (ModuleNotFoundError: No module named 'flake8_django')
cause The `flake8-django` package is not installed in the current Python environment where `flake8` is being run, or there's an environment issue.fixInstall `flake8-django`: `pip install flake8-django`. If in a virtual environment, ensure it's active. -
F821 undefined name '...' (often related to Django Models and Managers)
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.fixAdd `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. -
E501 line too long (80 > 79)
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.fixConfigure `flake8` to allow a longer line length by creating a `.flake8` file in your project root or adding to `pyproject.toml`: `[flake8] max-line-length = 88` (or your preferred length).
Warnings
- breaking 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.
- gotcha 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.
- gotcha 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.
- gotcha The DJ01 check warns against using `null=True` on string-based model fields like `CharField` and `TextField`, which is a common Django anti-pattern.
Install
-
pip install flake8-django flake8 Django
Imports
- flake8_django
No direct import needed. flake8 automatically discovers installed plugins.
Quickstart
import os
# Example Django model for demonstration
# This code would typically be in a models.py file
from django.db import models
class MyModel(models.Model):
# DJ01: Avoid using null=True on string-based fields
name = models.CharField(max_length=255, null=True, blank=True) # Will trigger DJ01
description = models.TextField(default='Default description')
class Meta:
# DJ08: Model does not define __str__ method (implicit)
pass
# To run flake8-django, save the above in a file (e.g., myapp/models.py)
# Then, from your project root in the terminal:
# pip install flake8 flake8-django
# flake8 .
# If specific rules like DJ10/DJ11 are desired:
# flake8 --select=E,F,W,C90,DJ,DJ10,DJ11 .