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
| Name | Type | Description |
|---|---|---|
| model | Model | The Django model class from which to extract fields. |
| fields | list = None | An optional list of field names. If provided, return only the named fields. |
| exclude | list = None | 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 | dict = None | A dictionary of model field names mapped to a widget class or instance. |
| formfield_callback | callable = None | A callable that takes a model field and returns a form field instance. |
| localized_fields | list = None | A list of names of fields which should be localized. |
| labels | dict = None | A dictionary of model field names mapped to a custom label string. |
| help_texts | dict = None | A dictionary of model field names mapped to a custom help text string. |
| error_messages | dict = None | A dictionary of model field names mapped to a dictionary of error messages. |
| field_classes | dict = None | A dictionary of model field names mapped to a specific form field class. |
| apply_limit_choices_to | boolean = True | A boolean indicating if limit_choices_to should be applied to a field's queryset. |
| form_declared_fields | dict = None | A dictionary of form fields created directly on a form to be included in the result. |
Returns
| Type | Description |
|---|---|
dict | A dictionary mapping field names to their corresponding form field instances. |