models.txt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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)
  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. See :ref:`modelforms-factory` for example usage.
  19. .. 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)
  20. Returns a ``FormSet`` class for the given ``model`` class.
  21. Arguments ``model``, ``form``, ``fields``, ``exclude``,
  22. ``formfield_callback`` and ``widgets`` are all passed through to
  23. :func:`~django.forms.models.modelform_factory`.
  24. Arguments ``formset``, ``extra``, ``max_num``, ``can_order``,
  25. ``can_delete`` and ``validate_max`` are passed through to
  26. :func:`~django.forms.formsets.formset_factory`. See :ref:`formsets` for
  27. details.
  28. See :ref:`model-formsets` for example usage.
  29. .. versionchanged:: 1.6
  30. The ``widgets`` and the ``validate_max`` parameters were added.
  31. .. 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)
  32. Returns an ``InlineFormSet`` using :func:`modelformset_factory` with
  33. defaults of ``formset=BaseInlineFormSet``, ``can_delete=True``, and
  34. ``extra=3``.
  35. If your model has more than one :class:`~django.db.models.ForeignKey` to
  36. the ``parent_model``, you must specify a ``fk_name``.
  37. See :ref:`inline-formsets` for example usage.
  38. .. versionchanged:: 1.6
  39. The ``widgets`` and the ``validate_max`` parameters were added.