Comprehensive type stubs for Django framework enabling static type checking with mypy
—
Django's forms system provides HTML form generation, data validation, and processing capabilities with comprehensive type support for form fields, widgets, and validation logic.
# Form base classes
from django import forms
from django.forms import Form, ModelForm, BaseForm
# Form fields
from django.forms import (
CharField, TextField, IntegerField, FloatField, DecimalField,
BooleanField, NullBooleanField, DateField, DateTimeField, TimeField,
EmailField, URLField, SlugField, RegexField, UUIDField,
ChoiceField, MultipleChoiceField, ModelChoiceField, ModelMultipleChoiceField,
FileField, ImageField, TypedChoiceField, TypedMultipleChoiceField
)
# Form widgets
from django.forms.widgets import (
TextInput, Textarea, NumberInput, EmailInput, URLInput,
PasswordInput, HiddenInput, CheckboxInput, Select, SelectMultiple,
RadioSelect, CheckboxSelectMultiple, FileInput, ClearableFileInput,
DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget
)
# Form validation
from django.forms import ValidationError
from django.core.exceptions import ValidationError as CoreValidationError
# Form utilities
from django.forms.formsets import formset_factory, modelformset_factory, inlineformset_factoryCore form classes providing structure for HTML form generation and data processing.
class BaseForm:
"""Base form implementation with field management and validation."""
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=None, label_suffix=None,
empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None): ...
def is_valid(self) -> bool: ...
def full_clean(self) -> None: ...
def clean(self) -> dict: ...
def has_changed(self) -> bool: ...
def changed_data(self) -> list: ...
def add_error(self, field: str, error) -> None: ...
def has_error(self, field: str, code: str = None) -> bool: ...
def non_field_errors(self): ...
class Form(BaseForm):
"""Standard form class for HTML form handling."""
def __init__(self, data=None, files=None, **kwargs): ...
class BaseModelForm(BaseForm):
"""Base class for forms tied to model instances."""
def __init__(self, data=None, files=None, instance=None, **kwargs): ...
def save(self, commit: bool = True): ...
class ModelForm(BaseModelForm):
"""Form automatically generated from a Django model."""
class Meta:
model = None
fields = None
exclude = None
widgets = None
localized_fields = None
labels = None
help_texts = None
error_messages = None
field_classes = NoneField types for form input validation and processing.
class Field:
"""Base field class for form fields."""
def __init__(self, required: bool = True, widget=None, label: str = None,
initial=None, help_text: str = '', error_messages: dict = None,
show_hidden_initial: bool = False, validators: list = None,
localize: bool = False, disabled: bool = False, label_suffix=None): ...
def clean(self, value): ...
def validate(self, value) -> None: ...
def run_validators(self, value) -> None: ...
def to_python(self, value): ...
def has_changed(self, initial, data) -> bool: ...
class CharField(Field):
"""Character/string input field."""
def __init__(self, max_length: int = None, min_length: int = None, strip: bool = True, **kwargs): ...
class IntegerField(Field):
"""Integer input field."""
def __init__(self, max_value: int = None, min_value: int = None, **kwargs): ...
class FloatField(Field):
"""Floating point number input field."""
def __init__(self, max_value: float = None, min_value: float = None, **kwargs): ...
class DecimalField(Field):
"""Decimal number input field."""
def __init__(self, max_value=None, min_value=None, max_digits: int = None, decimal_places: int = None, **kwargs): ...
class BooleanField(Field):
"""Boolean checkbox field."""
def __init__(self, **kwargs): ...
class NullBooleanField(Field):
"""Three-state boolean field (True/False/None)."""
def __init__(self, **kwargs): ...
class EmailField(CharField):
"""Email address input field with validation."""
def __init__(self, **kwargs): ...
class URLField(CharField):
"""URL input field with validation."""
def __init__(self, **kwargs): ...
class SlugField(CharField):
"""Slug input field for URL slugs."""
def __init__(self, **kwargs): ...
class RegexField(CharField):
"""Text field with regex validation."""
def __init__(self, regex, **kwargs): ...
class UUIDField(CharField):
"""UUID input field."""
def __init__(self, **kwargs): ...
class DateField(Field):
"""Date input field."""
def __init__(self, input_formats: list = None, **kwargs): ...
class TimeField(Field):
"""Time input field."""
def __init__(self, input_formats: list = None, **kwargs): ...
class DateTimeField(Field):
"""Date and time input field."""
def __init__(self, input_formats: list = None, **kwargs): ...
class DurationField(Field):
"""Duration/timedelta input field."""
def __init__(self, **kwargs): ...
class ChoiceField(Field):
"""Single choice selection field."""
def __init__(self, choices=(), **kwargs): ...
class TypedChoiceField(ChoiceField):
"""Choice field with type coercion."""
def __init__(self, coerce=None, empty_value: str = '', **kwargs): ...
class MultipleChoiceField(ChoiceField):
"""Multiple choice selection field."""
def __init__(self, **kwargs): ...
class TypedMultipleChoiceField(MultipleChoiceField):
"""Multiple choice field with type coercion."""
def __init__(self, coerce=None, **kwargs): ...
class FileField(Field):
"""File upload field."""
def __init__(self, max_length: int = None, allow_empty_file: bool = False, **kwargs): ...
class ImageField(FileField):
"""Image file upload field with validation."""
def __init__(self, **kwargs): ...
class FilePathField(ChoiceField):
"""File path selection field."""
def __init__(self, path: str, match: str = None, recursive: bool = False,
allow_files: bool = True, allow_folders: bool = False, **kwargs): ...
class JSONField(CharField):
"""JSON data input field."""
def __init__(self, encoder=None, decoder=None, **kwargs): ...Fields that combine multiple inputs or handle complex data structures.
class MultiValueField(Field):
"""Field that aggregates multiple fields."""
def __init__(self, fields: tuple, require_all_fields: bool = True, **kwargs): ...
def compress(self, data_list): ...
class ComboField(Field):
"""Field that validates against multiple field types."""
def __init__(self, fields: list, **kwargs): ...
class SplitDateTimeField(MultiValueField):
"""Date and time field split into separate inputs."""
def __init__(self, input_date_formats: list = None, input_time_formats: list = None, **kwargs): ...Fields specifically designed for working with Django models.
class ModelChoiceField(ChoiceField):
"""Choice field populated from model QuerySet."""
def __init__(self, queryset, empty_label: str = "---------", to_field_name: str = None, **kwargs): ...
class ModelMultipleChoiceField(MultipleChoiceField):
"""Multiple choice field populated from model QuerySet."""
def __init__(self, queryset, **kwargs): ...UI components for rendering form fields in HTML.
class Widget:
"""Base widget class for HTML form controls."""
def __init__(self, attrs: dict = None): ...
def render(self, name: str, value, attrs: dict = None, renderer=None) -> str: ...
def value_from_datadict(self, data: dict, files: dict, name: str): ...
def format_value(self, value): ...
class Input(Widget):
"""Base class for HTML input elements."""
input_type: str = None
template_name: str = 'django/forms/widgets/input.html'
class TextInput(Input):
"""Text input widget."""
input_type: str = 'text'
class NumberInput(Input):
"""Number input widget."""
input_type: str = 'number'
class EmailInput(Input):
"""Email input widget."""
input_type: str = 'email'
class URLInput(Input):
"""URL input widget."""
input_type: str = 'url'
class PasswordInput(Input):
"""Password input widget."""
input_type: str = 'password'
def __init__(self, attrs: dict = None, render_value: bool = False): ...
class HiddenInput(Input):
"""Hidden input widget."""
input_type: str = 'hidden'
class DateInput(Input):
"""Date input widget."""
input_type: str = 'date'
format_key: str = 'DATE_INPUT_FORMATS'
class DateTimeInput(Input):
"""DateTime input widget."""
input_type: str = 'datetime-local'
format_key: str = 'DATETIME_INPUT_FORMATS'
class TimeInput(Input):
"""Time input widget."""
input_type: str = 'time'
format_key: str = 'TIME_INPUT_FORMATS'
class Textarea(Widget):
"""Multi-line text input widget."""
template_name: str = 'django/forms/widgets/textarea.html'
def __init__(self, attrs: dict = None): ...
class CheckboxInput(Widget):
"""Checkbox input widget."""
template_name: str = 'django/forms/widgets/checkbox.html'
def __init__(self, attrs: dict = None, check_test=None): ...
class Select(Widget):
"""Select dropdown widget."""
template_name: str = 'django/forms/widgets/select.html'
def __init__(self, attrs: dict = None, choices=()): ...
class NullBooleanSelect(Select):
"""Select widget for null boolean field."""
class SelectMultiple(Select):
"""Multiple selection widget."""
template_name: str = 'django/forms/widgets/select_multiple.html'
def __init__(self, attrs: dict = None, choices=()): ...
class RadioSelect(Select):
"""Radio button selection widget."""
template_name: str = 'django/forms/widgets/radio.html'
class CheckboxSelectMultiple(SelectMultiple):
"""Multiple checkbox selection widget."""
template_name: str = 'django/forms/widgets/checkbox_select.html'
class FileInput(Input):
"""File upload widget."""
input_type: str = 'file'
template_name: str = 'django/forms/widgets/file.html'
class ClearableFileInput(FileInput):
"""File input with clear option."""
template_name: str = 'django/forms/widgets/clearable_file_input.html'Widgets that combine multiple form controls.
class MultiWidget(Widget):
"""Widget that combines multiple widgets."""
def __init__(self, widgets: tuple, attrs: dict = None): ...
def decompress(self, value): ...
class SplitDateTimeWidget(MultiWidget):
"""Widget for split date and time inputs."""
def __init__(self, attrs: dict = None, date_format: str = None, time_format: str = None): ...
class SplitHiddenDateTimeWidget(SplitDateTimeWidget):
"""Hidden widget for split date and time."""
class SelectDateWidget(Widget):
"""Date selection using separate dropdowns."""
def __init__(self, attrs: dict = None, years: list = None, months: dict = None, empty_label: str = None): ...Utility functions for form processing and generation.
def formset_factory(form, formset=None, extra: int = 1, can_order: bool = False,
can_delete: bool = False, max_num: int = None, min_num: int = None,
validate_max: bool = False, validate_min: bool = False, absolute_max: int = None) -> type:
"""Create a FormSet class for the given form class."""
def modelformset_factory(model, form=None, formfield_callback=None, formset=None,
extra: int = 1, can_delete: bool = False, can_order: bool = False,
max_num: int = None, min_num: int = None, fields=None, exclude=None,
widgets=None, validate_max: bool = False, validate_min: bool = False,
localized_fields=None, labels=None, help_texts=None,
error_messages=None, absolute_max: int = None, field_classes=None) -> type:
"""Create a ModelFormSet class for the given model."""
def inlineformset_factory(parent_model, model, form=None, formset=None, fk_name: str = None,
fields=None, exclude=None, extra: int = 3, can_order: bool = False,
can_delete: bool = True, max_num: int = None, min_num: int = None,
formfield_callback=None, widgets=None, validate_max: bool = False,
validate_min: bool = False, localized_fields=None, labels=None,
help_texts=None, error_messages=None, absolute_max: int = None,
field_classes=None) -> type:
"""Create an InlineFormSet class for the given models."""
def modelform_factory(model, form=None, fields=None, exclude=None, formfield_callback=None,
widgets=None, localized_fields=None, labels=None, help_texts=None,
error_messages=None, field_classes=None) -> type:
"""Create a ModelForm class for the given model."""
def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=None,
localized_fields=None, labels=None, help_texts=None, error_messages=None,
field_classes=None, apply_limit_choices_to: bool = True) -> dict:
"""Return form fields for the given model."""
def model_to_dict(instance, fields=None, exclude=None) -> dict:
"""Convert model instance to dictionary."""
def all_valid(formsets: list) -> bool:
"""Validate all formsets in a list."""Collections of forms for handling multiple instances.
class BaseFormSet:
"""Base class for form collections."""
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=None, form_kwargs=None): ...
def is_valid(self) -> bool: ...
def full_clean(self) -> None: ...
def clean(self) -> None: ...
def has_changed(self) -> bool: ...
def add_error(self, field: str, error) -> None: ...
def non_form_errors(self): ...
def total_form_count(self) -> int: ...
def initial_form_count(self) -> int: ...
class BaseModelFormSet(BaseFormSet):
"""Base class for model formsets."""
def __init__(self, data=None, files=None, queryset=None, **kwargs): ...
def save(self, commit: bool = True) -> list: ...
def save_existing_objects(self, commit: bool = True) -> list: ...
def save_new_objects(self, commit: bool = True) -> list: ...
def add_fields(self, form, index: int) -> None: ...
class BaseInlineFormSet(BaseModelFormSet):
"""Base class for inline formsets."""
def __init__(self, data=None, files=None, instance=None, **kwargs): ...Validation and error handling components.
class ValidationError(Exception):
"""Form validation error."""
def __init__(self, message, code: str = None, params: dict = None): ...
error_dict: dict
error_list: list
message: str
code: str
params: dict
class BoundField:
"""Field bound to form data."""
def __init__(self, form, field, name: str): ...
def as_widget(self, widget=None, attrs: dict = None, only_initial: bool = False) -> str: ...
def as_text(self, attrs: dict = None, **kwargs) -> str: ...
def as_textarea(self, attrs: dict = None, **kwargs) -> str: ...
def as_hidden(self, attrs: dict = None, **kwargs) -> str: ...
def value(self): ...
def label_tag(self, contents: str = None, attrs: dict = None, label_suffix=None) -> str: ...
class BoundWidget:
"""Widget bound to form data."""
def __init__(self, parent_widget, data, renderer): ...CSS and JavaScript asset management for forms and widgets.
class Media:
"""CSS and JavaScript media definition."""
def __init__(self, media=None, css: dict = None, js: tuple = None): ...
def render(self) -> str: ...
def render_js(self) -> list: ...
def render_css(self) -> list: ...
def absolute_path(self, path: str) -> str: ...
css: dict
js: tupleUtilities for handling form data and file uploads.
class MultiValueDict(dict):
"""Dictionary that can hold multiple values for the same key."""
def __init__(self, key_to_list_mapping=()): ...
def getlist(self, key: str, default: list = None) -> list: ...
def setlist(self, key: str, list_: list) -> None: ...
def appendlist(self, key: str, value) -> None: ...
def setlistdefault(self, key: str, default_list: list = None) -> list: ...
def lists(self): ...
def dict(self) -> dict: ...
class FileDict(MultiValueDict):
"""Dictionary for uploaded files."""
class QueryDict(MultiValueDict):
"""Dictionary for query string and POST data."""
def __init__(self, query_string: str = None, mutable: bool = False, encoding: str = None): ...
def urlencode(self, safe: str = None) -> str: ...Install with Tessl CLI
npx tessl i tessl/pypi-django-stubs