models.txt 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ====================
  2. Model Form Functions
  3. ====================
  4. .. module:: django.forms.models
  5. :synopsis: Django's functions for building model forms and formsets.
  6. .. function:: modelform_factory(model, form=ModelForm, fields=None, exclude=None, formfield_callback=None, widgets=None, localized_fields=None, labels=None, help_texts=None, error_messages=None)
  7. Returns a :class:`~django.forms.ModelForm` class for the given ``model``.
  8. You can optionally pass a ``form`` argument to use as a starting point for
  9. constructing the ``ModelForm``.
  10. ``fields`` is an optional list of field names. If provided, only the named
  11. fields will be included in the returned fields.
  12. ``exclude`` is an optional list of field names. If provided, the named
  13. fields will be excluded from the returned fields, even if they are listed
  14. in the ``fields`` argument.
  15. ``widgets`` is a dictionary of model field names mapped to a widget.
  16. ``formfield_callback`` is a callable that takes a model field and returns
  17. a form field.
  18. ``localized_fields`` is a list of names of fields which should be localized.
  19. ``labels`` is a dictionary of model field names mapped to a label.
  20. ``help_texts`` is a dictionary of model field names mapped to a help text.
  21. ``error_messages`` is a dictionary of model field names mapped to a
  22. dictionary of error messages.
  23. See :ref:`modelforms-factory` for example usage.
  24. You must provide the list of fields explicitly, either via keyword arguments
  25. ``fields`` or ``exclude``, or the corresponding attributes on the form's
  26. inner ``Meta`` class. See :ref:`modelforms-selecting-fields` for more
  27. information. Omitting any definition of the fields to use will result in
  28. an :exc:`~django.core.exceptions.ImproperlyConfigured` exception.
  29. .. versionchanged:: 1.8
  30. Previously, omitting the list of fields was allowed and resulted in
  31. a form with all fields of the model.
  32. .. function:: modelformset_factory(model, form=ModelForm, formfield_callback=None, formset=BaseModelFormSet, extra=1, can_delete=False, can_order=False, max_num=None, fields=None, exclude=None, widgets=None, validate_max=False, localized_fields=None, labels=None, help_texts=None, error_messages=None)
  33. Returns a ``FormSet`` class for the given ``model`` class.
  34. Arguments ``model``, ``form``, ``fields``, ``exclude``,
  35. ``formfield_callback``, ``widgets``, ``localized_fields``, ``labels``,
  36. ``help_texts``, and ``error_messages`` are all passed through to
  37. :func:`~django.forms.models.modelform_factory`.
  38. Arguments ``formset``, ``extra``, ``max_num``, ``can_order``,
  39. ``can_delete`` and ``validate_max`` are passed through to
  40. :func:`~django.forms.formsets.formset_factory`. See :ref:`formsets` for
  41. details.
  42. See :ref:`model-formsets` for example usage.
  43. .. versionchanged:: 1.6
  44. The ``widgets``, ``validate_max``, ``localized_fields``, ``labels``,
  45. ``help_texts``, and ``error_messages`` parameters were added.
  46. .. function:: inlineformset_factory(parent_model, model, form=ModelForm, formset=BaseInlineFormSet, fk_name=None, fields=None, exclude=None, extra=3, can_order=False, can_delete=True, max_num=None, formfield_callback=None, widgets=None, validate_max=False, localized_fields=None, labels=None, help_texts=None, error_messages=None)
  47. Returns an ``InlineFormSet`` using :func:`modelformset_factory` with
  48. defaults of ``formset=``:class:`~django.forms.models.BaseInlineFormSet`,
  49. ``can_delete=True``, and ``extra=3``.
  50. If your model has more than one :class:`~django.db.models.ForeignKey` to
  51. the ``parent_model``, you must specify a ``fk_name``.
  52. See :ref:`inline-formsets` for example usage.
  53. .. versionchanged:: 1.6
  54. The ``widgets``, ``validate_max`` and ``localized_fields``, ``labels``,
  55. ``help_texts``, and ``error_messages`` parameters were added.