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
[Zope schemas](https://zopeschema.readthedocs.io) are a database-neutral and form-library-neutral way to
22
-
describe Python data models.
22
+
describe Python data models. Schemas extend the notion of interfaces to detailed descriptions of Attributes (but not methods). Every schema is an interface and specifies the public fields of an object. A field roughly corresponds to an attribute of a Python object. But a Field provides space for at least a title and a description. It can also constrain its value and provide a validation method. Besides you can optionally specify characteristics such as its value being read-only or not required.
23
23
24
24
Plone uses Zope schemas for these purposes:
25
25
26
26
- to describe persistent data models;
27
27
- to describe HTML form data;
28
+
- to describe Plone configuration data
28
29
- to describe ZCML configuration data.
29
30
30
31
Since Zope schemas are not bound to e.g. a SQL database engine, it gives
@@ -102,95 +103,19 @@ class ICheckoutAddress(zope.interface.Interface):
102
103
103
104
This schema can be used in {ref}`classic-ui-forms-label` and Dexterity {ref}`backend-content-types-label` data models.
104
105
105
-
106
-
## Advanced
107
-
108
-
We can use this class to store data based on our model definition in the ZODB
109
-
database.
110
-
111
-
We use `zope.schema.fieldproperty.FieldProperty` to bind
112
-
persistent class attributes to the data definition.
113
-
114
-
Example:
115
-
116
-
```
117
-
from persistent import Persistent # Automagical ZODB persistent object
118
-
from zope.schema.fieldproperty import FieldProperty
`zope.schema` and `plone.schema` provide a very comprehensive set of {ref}`backend-fields-label` out of the box.
151
-
Finding good documentation for them, however, can be challenging. Here are
152
-
some starting points:
153
-
154
-
- {ref}`reference list of fields used in Plone <backend-fields-label>`
155
-
156
-
157
-
## Using schemas as data models
158
-
159
-
Based on the example data model above, we can use it in e.g. content type
160
-
{doc}`browser views </develop/plone/views/browserviews>` to store arbitrary data as content
161
-
type attributes.
162
-
163
-
Example:
164
-
106
+
```{note}
107
+
In Dexterity {ref}`backend-content-types-label` the base class for a schema is `plone.supermodel.model.Schema`.
108
+
This provides functionalities to export and import schemas via XML and TTW editor.
165
109
```
166
-
class MyView(BrowserView):
167
-
""" Connect this view to your content type using a ZCML declaration.
168
-
"""
169
-
170
-
def __call__(self):
171
-
# Get the content item which this view was invoked on:
172
-
context = self.context.aq_inner
173
-
174
-
# Store a new address in it as the ``test_address`` attribute
175
-
context.test_address = CheckoutAddress()
176
-
context.test_address.first_name = u"Mikko"
177
-
context.test_address.last_name = u"Ohtamaa"
178
110
179
-
# Note that you can still add arbitrary attributes to any
180
-
# persistent object. They are simply not validated, as they
181
-
# don't go through the ``zope.schema`` FieldProperty
182
-
# declarations.
183
-
# Do not do this, you will regret it later.
184
-
context.test_address.arbitary_attribute = u"Don't do this!"
185
-
```
186
111
187
-
## Field constructor parameters
112
+
###Field constructor parameters
188
113
189
114
The `Field` base class defines a list of standard parameters that you can
190
115
use to construct schema fields. Each subclass of `Field` will have its own
191
116
set of possible parameters in addition to this.
192
117
193
-
See the full list [here](http://docs.zope.org/zope3/Code/zope/schema/_bootstrapfields/Field/index.html).
118
+
See [IField interface](https://zopeschema.readthedocs.io/en/latest/api.html#zope.schema.interfaces.IField) and [field implementation](https://zopeschema.readthedocs.io/en/latest/api.html#field-implementations) in `zope.schema` documentation for details.
194
119
195
120
Title
196
121
@@ -220,9 +145,12 @@ is probably not what you intend. Instead, initialize objects in the
220
145
221
146
In particular, dangerous defaults are: `default=[]`, `default={}`,
222
147
`default=SomeObject()`.
148
+
149
+
Use `defaultFactory=get_default_name` instead!
223
150
```
224
151
225
-
## Schema introspection
152
+
153
+
### Schema introspection
226
154
227
155
The `zope.schema._schema` module provides some introspection functions:
228
156
@@ -247,7 +175,7 @@ class IMyInterface(zope.interface.Interface):
247
175
fields = zope.schema.getFields(IMyInterface)
248
176
```
249
177
250
-
### Dumping schema data
178
+
####Dumping schema data
251
179
252
180
Below is an example how to extract all schema defined fields from an object.
253
181
@@ -276,7 +204,7 @@ def dump_schemed_data(obj):
276
204
return out
277
205
```
278
206
279
-
### Finding the schema for a Dexterity type
207
+
####Finding the schema for a Dexterity type
280
208
281
209
When trying to introspect a Dexterity type, you can get a reference to the schema thus:
0 commit comments