You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"property=og:description": "Content type defaults"
6
+
"property=og:title": "Content type defaults"
7
+
"keywords": "Plone, Content type, defaults"
8
8
---
9
9
10
10
# Defaults
11
11
12
-
**Default values for fields on add forms**
12
+
This chapter describes the default values for fields on add forms.
13
13
14
-
It is often useful to calculate a default value for a field. This value
15
-
will be used on the add form, before the field is set.
14
+
It is often useful to calculate a default value for a field.
15
+
This value will be used on the add form before the field is set.
16
16
17
-
To continue with our conference example, let’s set the default values
18
-
for the `start` and `end` dates to one week in the future and ten days
19
-
in the future, respectively. We can do this by adding the following to
20
-
`program.py`:
17
+
To continue with our conference example, let's set the default values for the `start` and `end` dates to one week in the future and ten days in the future, respectively.
18
+
We can do this by adding the following to {file}`program.py`.
We also need to modify IProgram so the `start` and `end` fields
33
-
use these functions as their `defaultFactory`:
30
+
We also need to modify `IProgram` so the `start` and `end` fields use these functions as their `defaultFactory`.
34
31
35
32
```python
36
33
classIProgram(model.Schema):
@@ -48,15 +45,12 @@ class IProgram(model.Schema):
48
45
)
49
46
```
50
47
51
-
The `defaultFactory` is a function that will be called when the add form
52
-
is loaded to determine the default value.
48
+
The `defaultFactory` is a function that will be called when the add form is loaded to determine the default value.
53
49
54
-
The value returned by the method should be a value that’s allowable for
55
-
the field. In the case of `Datetime` fields, that’s a Python `datetime`
56
-
object.
50
+
The value returned by the method should be a value that's allowable for the field.
51
+
In the case of `Datetime` fields, that's a Python `datetime` object.
57
52
58
-
It is also possible to write a context-aware default factory that will be
59
-
passed the container for which the add form is being displayed:
53
+
It is also possible to write a context-aware default factory that will be passed the container for which the add form is being displayed.
60
54
61
55
```python
62
56
from zope.interface import provider
@@ -67,10 +61,7 @@ def getContainerId(context):
67
61
return context.getId()
68
62
```
69
63
70
-
It is possible to provide different default values depending on the type
71
-
of context, a request layer, the type of form, or the type of widget
72
-
used. See the [z3c.form] documentation for more details.
64
+
It is possible to provide different default values depending on the type of context, a request layer, the type of form, or the type of widget used.
65
+
See the [z3c.form](https://z3cform.readthedocs.io/en/latest/advanced/validator.html#look-up-value-from-default-adapter) documentation for more details.
73
66
74
-
We’ll cover creating custom forms later in this manual.
0 commit comments