Module models
Helper functions for creating Form classes from Django models and database field objects.
source linkClasses
-
A formset for child objects related to a parent.
-
-
A ``FormSet`` for editing a queryset and/or adding new objects to it.
-
A basic integer field that deals with validating the given value to a given parent instance in an inline.
-
A ChoiceField whose choices are a model QuerySet.
-
-
-
-
-
-
A MultipleChoiceField whose choices are a model QuerySet.
Functions
▷ def construct_instance(form, instance, fields=None, exclude=None) Construct and return a model instance from the bound ``form``'s ``cleaned_data``, but do not save the returned instance to the database.
▶ def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=None, ...) Return a dictionary containing form fields 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=True,
)
``fields`` is an optional list of field names. If provided, return only the
named fields.
``exclude`` is an optional list of field names. If provided, exclude the
named fields from the returned fields, even if they are listed in the
``fields`` argument.
``widgets`` is a dictionary of model field names mapped to a widget.
``formfield_callback`` is a callable that takes a model field and returns
a form field.
``localized_fields`` is a list of names of fields which should be localized.
``labels`` is a dictionary of model field names mapped to a label.
``help_texts`` is a dictionary of model field names mapped to a help text.
``error_messages`` is a dictionary of model field names mapped to a
dictionary of error messages.
``field_classes`` is a dictionary of model field names mapped to a form
field class.
``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
should be applied to a field's queryset.
▶ def inlineformset_factory(parent_model, model, form=ModelForm, formset=BaseInlineFormSet, fk_name=None, ...) Return an ``InlineFormSet`` for the given kwargs.
▶ def model_to_dict(instance, fields=None, exclude=None) Return a dict containing the data in ``instance`` suitable for passing as a Form's ``initial`` keyword argument.
``fields`` is an optional list of field names. If provided, return only the
named.
``exclude`` is an optional list of field names. If provided, exclude the
named from the returned dict, even if they are listed in the ``fields``
argument.
▶ def modelform_factory(model, form=ModelForm, fields=None, exclude=None, formfield_callback=None, ...) Return a ModelForm containing form fields for the given model. You can optionally pass a `form` argument to use as a starting point for constructing the ModelForm.
▶ def modelformset_factory(model, form=ModelForm, formfield_callback=None, formset=BaseModelFormSet, ...) Return a FormSet class for the given Django model class.