Return a dictionary containing form fields for the given model. ``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 the
(
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,
form_declared_fields=None,
)
| 139 | |
| 140 | |
| 141 | def fields_for_model( |
| 142 | model, |
| 143 | fields=None, |
| 144 | exclude=None, |
| 145 | widgets=None, |
| 146 | formfield_callback=None, |
| 147 | localized_fields=None, |
| 148 | labels=None, |
| 149 | help_texts=None, |
| 150 | error_messages=None, |
| 151 | field_classes=None, |
| 152 | *, |
| 153 | apply_limit_choices_to=True, |
| 154 | form_declared_fields=None, |
| 155 | ): |
| 156 | """ |
| 157 | Return a dictionary containing form fields for the given model. |
| 158 | |
| 159 | ``fields`` is an optional list of field names. If provided, return only the |
| 160 | named fields. |
| 161 | |
| 162 | ``exclude`` is an optional list of field names. If provided, exclude the |
| 163 | named fields from the returned fields, even if they are listed in the |
| 164 | ``fields`` argument. |
| 165 | |
| 166 | ``widgets`` is a dictionary of model field names mapped to a widget. |
| 167 | |
| 168 | ``formfield_callback`` is a callable that takes a model field and returns |
| 169 | a form field. |
| 170 | |
| 171 | ``localized_fields`` is a list of names of fields which should be |
| 172 | localized. |
| 173 | |
| 174 | ``labels`` is a dictionary of model field names mapped to a label. |
| 175 | |
| 176 | ``help_texts`` is a dictionary of model field names mapped to a help text. |
| 177 | |
| 178 | ``error_messages`` is a dictionary of model field names mapped to a |
| 179 | dictionary of error messages. |
| 180 | |
| 181 | ``field_classes`` is a dictionary of model field names mapped to a form |
| 182 | field class. |
| 183 | |
| 184 | ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to |
| 185 | should be applied to a field's queryset. |
| 186 | |
| 187 | ``form_declared_fields`` is a dictionary of form fields created directly on |
| 188 | a form. |
| 189 | """ |
| 190 | form_declared_fields = form_declared_fields or {} |
| 191 | field_dict = {} |
| 192 | ignored = [] |
| 193 | opts = model._meta |
| 194 | # Avoid circular import |
| 195 | from django.db.models import Field as ModelField |
| 196 | |
| 197 | sortable_private_fields = [ |
| 198 | f for f in opts.private_fields if isinstance(f, ModelField) |