A Django model form or serializer exposes every model field with __all__ or exclude
What it finds
fields = "__all__" and exclude = [...] in the Meta of a ModelForm or ModelSerializer accept every field of the model that is not excluded, including fields added to the model later, such as an is_admin flag, which users can then set by posting them. Listing the fields keeps the form to what it is meant to change.
From the rule's source, django_form_all_fields.rs.
from django import forms class BookForm(forms.ModelForm): class Meta: model = Book fields = "__all__"tx0 reports: __all__ accepts every model field class AuthorForm(forms.ModelForm): class Meta: model = Author exclude = ["created"]tx0 reports: exclude accepts every model field not listed class SafeForm(forms.ModelForm): class Meta: model = Book fields = ["title"] Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:django-form-all-fields" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:django-form-all-fields] reason="..."tx0 explain tx0/python:django-form-all-fields shows the same in the terminal. Found a false positive? Report it.