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
Add example for using swagger_schema_fields for a Field (#494)
* Add example for using swagger_schema_fields for a Field
* Mention that Meta class can be added to fields as well
* Reference the DRF docs on how to add validation to serializers
Copy file name to clipboardExpand all lines: docs/custom_spec.rst
+62-1Lines changed: 62 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -212,7 +212,8 @@ Schema generation of ``serializers.SerializerMethodField`` is supported in two w
212
212
Serializer ``Meta`` nested class
213
213
********************************
214
214
215
-
You can define some per-serializer options by adding a ``Meta`` class to your serializer, e.g.:
215
+
You can define some per-serializer or per-field options by adding a ``Meta`` class to your ``Serializer`` or
216
+
serializer ``Field``, e.g.:
216
217
217
218
.. code-block:: python
218
219
@@ -236,6 +237,64 @@ The available options are:
236
237
which are converted to Swagger ``Schema`` attribute names according to :func:`.make_swagger_name`.
237
238
Attribute names and values must conform to the `OpenAPI 2.0 specification <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject>`_.
238
239
240
+
Suppose you wanted to model an email using a `JSONField` to store the subject and body for performance reasons:
241
+
242
+
.. code-block:: python
243
+
244
+
from django.contrib.postgres.fields import JSONField
245
+
246
+
classEmail(models.Model):
247
+
# Store data as JSON, but the data should be made up of
248
+
# an object that has two properties, "subject" and "body"
249
+
# Example:
250
+
# {
251
+
# "subject": "My Title",
252
+
# "body": "The body of the message.",
253
+
# }
254
+
message = JSONField()
255
+
256
+
To instruct ``drf-yasg`` to output an OpenAPI schema that matches this, create a custom ``JSONField``:
257
+
258
+
.. code-block:: python
259
+
260
+
classEmailMessageField(serializers.JSONField):
261
+
classMeta:
262
+
swagger_schema_fields = {
263
+
"type": openapi.TYPE_OBJECT,
264
+
"title": "Email",
265
+
"properties": {
266
+
"subject": openapi.Schema(
267
+
title="Email subject",
268
+
type=openapi.TYPE_STRING,
269
+
),
270
+
"body": openapi.Schema(
271
+
title="Email body",
272
+
type=openapi.TYPE_STRING,
273
+
),
274
+
},
275
+
"required": ["subject", "body"],
276
+
}
277
+
278
+
classEmailSerializer(ModelSerializer):
279
+
classMeta:
280
+
model = Email
281
+
fields ="__all__"
282
+
283
+
message = EmailMessageField()
284
+
285
+
.. Warning::
286
+
287
+
Overriding a default ``Field`` generated by a ``ModelSerializer`` will also override automatically
288
+
generated validators for that ``Field``. To add ``Serializer`` validation back in manually, see the relevant
289
+
`DRF Validators`_ and `DRF Fields`_ documentation.
290
+
291
+
One example way to do this is to set the ``default_validators`` attribute on a field.
292
+
293
+
.. code-block:: python
294
+
295
+
classEmailMessageField(serializers.JSONField):
296
+
default_validators = [my_custom_email_validator]
297
+
...
239
298
240
299
*************************
241
300
Subclassing and extending
@@ -389,3 +448,5 @@ A second example, of a :class:`~.inspectors.FieldInspector` that removes the ``t
389
448
390
449
391
450
.. _Python 3 type hinting: https://docs.python.org/3/library/typing.html
0 commit comments