MCPcopy
hub / github.com/django/django / modelform_factory

Function modelform_factory

django/forms/models.py:589–675  ·  view source on GitHub ↗

Return a ModelForm containing form fields for the given model. You can optionally pass a `form` argument to use as a starting point for constructing the ModelForm. ``fields`` is an optional list of field names. If provided, include only the named fields in the returned fields.

(
    model,
    form=ModelForm,
    fields=None,
    exclude=None,
    formfield_callback=None,
    widgets=None,
    localized_fields=None,
    labels=None,
    help_texts=None,
    error_messages=None,
    field_classes=None,
)

Source from the content-addressed store, hash-verified

587
588
589def modelform_factory(
590 model,
591 form=ModelForm,
592 fields=None,
593 exclude=None,
594 formfield_callback=None,
595 widgets=None,
596 localized_fields=None,
597 labels=None,
598 help_texts=None,
599 error_messages=None,
600 field_classes=None,
601):
602 """
603 Return a ModelForm containing form fields for the given model. You can
604 optionally pass a `form` argument to use as a starting point for
605 constructing the ModelForm.
606
607 ``fields`` is an optional list of field names. If provided, include only
608 the named fields in the returned fields. If omitted or '__all__', use all
609 fields.
610
611 ``exclude`` is an optional list of field names. If provided, exclude the
612 named fields from the returned fields, even if they are listed in the
613 ``fields`` argument.
614
615 ``widgets`` is a dictionary of model field names mapped to a widget.
616
617 ``localized_fields`` is a list of names of fields which should be
618 localized.
619
620 ``formfield_callback`` is a callable that takes a model field and returns
621 a form field.
622
623 ``labels`` is a dictionary of model field names mapped to a label.
624
625 ``help_texts`` is a dictionary of model field names mapped to a help text.
626
627 ``error_messages`` is a dictionary of model field names mapped to a
628 dictionary of error messages.
629
630 ``field_classes`` is a dictionary of model field names mapped to a form
631 field class.
632 """
633 # Create the inner Meta class. FIXME: ideally, we should be able to
634 # construct a ModelForm without creating and passing in a temporary
635 # inner class.
636
637 # Build up a list of attributes that the Meta object will have.
638 attrs = {"model": model}
639 if fields is not None:
640 attrs["fields"] = fields
641 if exclude is not None:
642 attrs["exclude"] = exclude
643 if widgets is not None:
644 attrs["widgets"] = widgets
645 if localized_fields is not None:
646 attrs["localized_fields"] = localized_fields

Calls 1