diff options
| author | Kamil Turek <kamil.turek@hotmail.com> | 2022-08-04 20:39:12 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-08-08 09:46:05 +0200 |
| commit | e03cdf76e78ea992763df4d3e16217d298929301 (patch) | |
| tree | ffb741971fc06e0c8726019908d8c027e909c24c /docs/topics/forms | |
| parent | 88e67a54b7ed0210c11523a337b498aadb2f5187 (diff) | |
Fixed #31721 -- Allowed ModelForm meta to specify form fields.
Diffstat (limited to 'docs/topics/forms')
| -rw-r--r-- | docs/topics/forms/modelforms.txt | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index 3332709089..9000ff24f4 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -548,8 +548,8 @@ the ``name`` field:: }, } -You can also specify ``field_classes`` to customize the type of fields -instantiated by the form. +You can also specify ``field_classes`` or ``formfield_callback`` to customize +the type of fields instantiated by the form. For example, if you wanted to use ``MySlugFormField`` for the ``slug`` field, you could do the following:: @@ -565,6 +565,21 @@ field, you could do the following:: 'slug': MySlugFormField, } +or:: + + from django.forms import ModelForm + from myapp.models import Article + + def formfield_for_dbfield(db_field, **kwargs): + if db_field.name == "slug": + return MySlugFormField() + return db_field.formfield(**kwargs) + + class ArticleForm(ModelForm): + class Meta: + model = Article + fields = ["pub_date", "headline", "content", "reporter", "slug"] + formfield_callback = formfield_for_dbfield Finally, if you want complete control over of a field -- including its type, validators, required, etc. -- you can do this by declaratively specifying @@ -638,6 +653,9 @@ the field declaratively and setting its ``validators`` parameter:: See the :doc:`form field documentation </ref/forms/fields>` for more information on fields and their arguments. +.. versionchanged:: 4.2 + + The ``Meta.formfield_callback`` attribute was added. Enabling localization of fields ------------------------------- |
