Skip to main content

fields_for_model

Return a dictionary containing form fields for the given model.

def fields_for_model(
model: Model,
fields: list = None,
exclude: list = None,
widgets: dict = None,
formfield_callback: callable = None,
localized_fields: list = None,
labels: dict = None,
help_texts: dict = None,
error_messages: dict = None,
field_classes: dict = None,
apply_limit_choices_to: boolean = True,
form_declared_fields: dict = None
) - > dict

Return a dictionary containing form fields for the given model.

Parameters

NameTypeDescription
modelModelThe Django model class from which to extract fields.
fieldslist = NoneAn optional list of field names. If provided, return only the named fields.
excludelist = NoneAn optional list of field names. If provided, exclude the named fields from the returned fields, even if they are listed in the fields argument.
widgetsdict = NoneA dictionary of model field names mapped to a widget class or instance.
formfield_callbackcallable = NoneA callable that takes a model field and returns a form field instance.
localized_fieldslist = NoneA list of names of fields which should be localized.
labelsdict = NoneA dictionary of model field names mapped to a custom label string.
help_textsdict = NoneA dictionary of model field names mapped to a custom help text string.
error_messagesdict = NoneA dictionary of model field names mapped to a dictionary of error messages.
field_classesdict = NoneA dictionary of model field names mapped to a specific form field class.
apply_limit_choices_toboolean = TrueA boolean indicating if limit_choices_to should be applied to a field's queryset.
form_declared_fieldsdict = NoneA dictionary of form fields created directly on a form to be included in the result.

Returns

TypeDescription
dictA dictionary mapping field names to their corresponding form field instances.