Code

Ticket #11740: modelforms_patch.diff

File modelforms_patch.diff, 1.6 KB (added by severian, 5 years ago)

Documentation patch

Line 
1Index: docs/topics/forms/modelforms.txt
2===================================================================
3--- docs/topics/forms/modelforms.txt    (revision 11468)
4+++ docs/topics/forms/modelforms.txt    (working copy)
5@@ -371,6 +371,35 @@
6    ...     class Meta:
7    ...         model = Article
8 
9+.. note::
10+
11+    If you explicitly instantiate a form field like this, Django assumes that you
12+    want to completely define its behavior; therefore, default attributes (such as
13+    ``max_length`` or ``required``) are not drawn from the corresponding model. If
14+    you want to maintain the behavior specified in the model, you must set the
15+    relevant arguments explicitly when declaring the form field.
16+
17+    For example, if the ``Article`` model looks like this::
18+
19+        class Article(models.Model):
20+            headline = models.CharField(max_length=200, null=True, blank=True,
21+                                        help_text="Use puns liberally")
22+            content = models.TextField()
23+
24+    and you want to do some custom validation for ``headline``, while keeping
25+    the ``blank`` and ``help_text`` values as specified, you might define
26+    ``ArticleForm`` like this::
27+
28+        class ArticleForm(ModelForm):
29+            headline = MyFormField(max_length=200, required=False,
30+                                   help_text="Use puns liberally")
31+
32+            class Meta:
33+                model = Article
34+
35+    See the :ref:`form field documentation <ref-forms-fields>` for more information
36+    on fields and their arguments.
37+
38 Changing the order of fields
39 ----------------------------
40