Skip to content

Commit 6a1166d

Browse files
johnthagenaxnsan12
authored andcommitted
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
1 parent b700191 commit 6a1166d

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

docs/custom_spec.rst

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ Schema generation of ``serializers.SerializerMethodField`` is supported in two w
212212
Serializer ``Meta`` nested class
213213
********************************
214214

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.:
216217

217218
.. code-block:: python
218219
@@ -236,6 +237,64 @@ The available options are:
236237
which are converted to Swagger ``Schema`` attribute names according to :func:`.make_swagger_name`.
237238
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>`_.
238239

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+
class Email(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+
class EmailMessageField(serializers.JSONField):
261+
class Meta:
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+
class EmailSerializer(ModelSerializer):
279+
class Meta:
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+
class EmailMessageField(serializers.JSONField):
296+
default_validators = [my_custom_email_validator]
297+
...
239298
240299
*************************
241300
Subclassing and extending
@@ -389,3 +448,5 @@ A second example, of a :class:`~.inspectors.FieldInspector` that removes the ``t
389448

390449

391450
.. _Python 3 type hinting: https://docs.python.org/3/library/typing.html
451+
.. _DRF Validators: https://www.django-rest-framework.org/api-guide/validators/
452+
.. _DRF Fields: https://www.django-rest-framework.org/api-guide/fields/#validators

0 commit comments

Comments
 (0)