models.txt 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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)
  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``, and
  25. ``can_delete`` are passed through to ``formset_factory``. See
  26. :ref:`formsets` for details.
  27. See :ref:`model-formsets` for example usage.
  28. .. versionchanged:: 1.6
  29. The widgets parameter was added.
  30. .. 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)
  31. Returns an ``InlineFormSet`` using :func:`modelformset_factory` with
  32. defaults of ``formset=BaseInlineFormSet``, ``can_delete=True``, and
  33. ``extra=3``.
  34. If your model has more than one :class:`~django.db.models.ForeignKey` to
  35. the ``parent_model``, you must specify a ``fk_name``.
  36. See :ref:`inline-formsets` for example usage.
  37. .. versionchanged:: 1.6
  38. The widgets parameter was added.