models.txt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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)
  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. ``localized_fields`` is a list of names of fields which should be localized.
  17. ``formfield_callback`` is a callable that takes a model field and returns
  18. a form field.
  19. See :ref:`modelforms-factory` for example usage.
  20. .. versionchanged:: 1.6
  21. You must provide the list of fields explicitly, either via keyword arguments
  22. ``fields`` or ``exclude``, or the corresponding attributes on the form's
  23. inner ``Meta`` class. See :ref:`modelforms-selecting-fields` for more
  24. information. Omitting any definition of the fields to use will result in all
  25. fields being used, but this behavior is deprecated.
  26. The ``localized_fields`` parameter was added.
  27. .. 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)
  28. Returns a ``FormSet`` class for the given ``model`` class.
  29. Arguments ``model``, ``form``, ``fields``, ``exclude``,
  30. ``formfield_callback``, ``widgets`` and ``localized_fields`` are all passed through to
  31. :func:`~django.forms.models.modelform_factory`.
  32. Arguments ``formset``, ``extra``, ``max_num``, ``can_order``,
  33. ``can_delete`` and ``validate_max`` are passed through to
  34. :func:`~django.forms.formsets.formset_factory`. See :ref:`formsets` for
  35. details.
  36. See :ref:`model-formsets` for example usage.
  37. .. versionchanged:: 1.6
  38. The ``widgets``, ``validate_max`` and ``localized_fields`` parameters were added.
  39. .. 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)
  40. Returns an ``InlineFormSet`` using :func:`modelformset_factory` with
  41. defaults of ``formset=BaseInlineFormSet``, ``can_delete=True``, and
  42. ``extra=3``.
  43. If your model has more than one :class:`~django.db.models.ForeignKey` to
  44. the ``parent_model``, you must specify a ``fk_name``.
  45. See :ref:`inline-formsets` for example usage.
  46. .. versionchanged:: 1.6
  47. The ``widgets``, ``validate_max`` and ``localized_fields`` parameters were added.