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
If you only want a subset of the default fields to be used in a model serializer, you can do so using `fields` or `exclude` options, just as you would with a `ModelForm`.
463
463
@@ -499,7 +499,7 @@ You can add extra fields to a `ModelSerializer` or override the default fields b
499
499
500
500
Extra fields can correspond to any property or callable on the model.
501
501
502
-
## Specifying which fields should be read-only
502
+
## Specifying readonly fields
503
503
504
504
You may wish to specify multiple fields as read-only. Instead of adding each field explicitly with the `read_only=True` attribute, you may use the shortcut Meta option, `read_only_fields`.
505
505
@@ -528,7 +528,7 @@ Please review the [Validators Documentation](/api-guide/validators/) for details
528
528
---
529
529
530
530
531
-
## Specifying additional keyword arguments for fields.
531
+
## Additional keyword arguments
532
532
533
533
There is also a shortcut allowing you to specify arbitrary additional keyword arguments on fields, using the `extra_kwargs` option. Similarly to `read_only_fields` this means you do not need to explicitly declare the field on the serializer.
534
534
@@ -567,31 +567,62 @@ The inner `Meta` class on serializers is not inherited from parent classes by de
567
567
568
568
Typically we would recommend *not* using inheritance on inner Meta classes, but instead declaring all options explicitly.
569
569
570
-
## Advanced `ModelSerializer` usage
570
+
## Customizing field mappings
571
571
572
572
The ModelSerializer class also exposes an API that you can override in order to alter how serializer fields are automatically determined when instantiating the serializer.
573
573
574
-
#### `.serializer_field_mapping`
574
+
Normally if a `ModelSerializer` does not generate the fields you need by default the you should either add them to the class explicitly, or simply use a regular `Serializer` class instead. However in some cases you may want to create a new base class that defines how the serializer fields are created for any given model.
575
+
576
+
### `.serializer_field_mapping`
575
577
576
578
A mapping of Django model classes to REST framework serializer classes. You can override this mapping to alter the default serializer classes that should be used for each model class.
577
579
578
-
####`.serializer_relational_field`
580
+
### `.serializer_relational_field`
579
581
580
582
This property should be the serializer field class, that is used for relational fields by default. For `ModelSerializer` this defaults to `PrimaryKeyRelatedField`. For `HyperlinkedModelSerializer` this defaults to `HyperlinkedRelatedField`.
581
583
582
-
#### The build field methods
584
+
### The field_class and field_kwargs API
585
+
586
+
The following methods are called to determine the class and keyword arguments for each field that should be automatically included on the serializer. Each of these methods should return a two tuple of `(field_class, field_kwargs)`.
0 commit comments