... | @@ -18,7 +18,7 @@ Of course, the best would still be to go trough [Django Rest Framework (DRF)'s t |
... | @@ -18,7 +18,7 @@ Of course, the best would still be to go trough [Django Rest Framework (DRF)'s t |
|
6. [The serializers](#6-the-serializers)
|
|
6. [The serializers](#6-the-serializers)
|
|
6.1. [Default behavior](#61-default-behavior)
|
|
6.1. [Default behavior](#61-default-behavior)
|
|
6.2. [Django models relation serialization](#62-django-models-relation-serialization)
|
|
6.2. [Django models relation serialization](#62-django-models-relation-serialization)
|
|
6.3. [Control the outputed field](#63-control-the-outputed-field)
|
|
6.3. [Control the output field](#63-control-the-output-field)
|
|
6.4. [Specifying a non-default serializer field](#64-specifying-a-non-default-serializer-field)
|
|
6.4. [Specifying a non-default serializer field](#64-specifying-a-non-default-serializer-field)
|
|
6.5. [Add custom serializer fields](#65-add-custom-serializer-fields)
|
|
6.5. [Add custom serializer fields](#65-add-custom-serializer-fields)
|
|
6.6. [Using serializers as serializer fields](#66-using-serializers-as-serializer-fields)
|
|
6.6. [Using serializers as serializer fields](#66-using-serializers-as-serializer-fields)
|
... | @@ -316,7 +316,7 @@ The data that is extracted from the Django objects need to be formatted so the r |
... | @@ -316,7 +316,7 @@ The data that is extracted from the Django objects need to be formatted so the r |
|
|
|
|
|
## 6.1 - Default behavior
|
|
## 6.1 - Default behavior
|
|
|
|
|
|
DRF provides some base serializers that have an automatic behavior on certain kind of fields. For example, for a *CharField*, the string is outputed ; for a *DateTimeField*, the date is casted to a string using [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) format ; for a *BooleanField*, the value is casted to `true` or `false` ; for an *IntegerField*, the value is casted to a string, ... Here is an example:
|
|
DRF provides some base serializers that have an automatic behavior on certain kind of fields. For example, for a *CharField*, the string is output ; for a *DateTimeField*, the date is casted to a string using [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) format ; for a *BooleanField*, the value is casted to `true` or `false` ; for an *IntegerField*, the value is casted to a string, ... Here is an example:
|
|
|
|
|
|
```python
|
|
```python
|
|
import json
|
|
import json
|
... | @@ -429,7 +429,7 @@ print(json.dumps(seria.data), indent=2) |
... | @@ -429,7 +429,7 @@ print(json.dumps(seria.data), indent=2) |
|
This serializer is a simple subclass of `rest_framework.serializers.HyperlinkedModelSerializer` to support the use of namespaced URL (which is not supported because considered not useful for an API and also I think to complex to implement for any namespacing). This serializer will behave exactly as the other one but all URL resolving for related models is done in the namespace 'api', where are all Re2o's API URLs. Without this modification, either the namespacing need to be remove for the whole API or the serializer will fail resolving the URL, so consider using this serializer instead of `rest_framework.serializers.HyperlinkedModelSerializer`.
|
|
This serializer is a simple subclass of `rest_framework.serializers.HyperlinkedModelSerializer` to support the use of namespaced URL (which is not supported because considered not useful for an API and also I think to complex to implement for any namespacing). This serializer will behave exactly as the other one but all URL resolving for related models is done in the namespace 'api', where are all Re2o's API URLs. Without this modification, either the namespacing need to be remove for the whole API or the serializer will fail resolving the URL, so consider using this serializer instead of `rest_framework.serializers.HyperlinkedModelSerializer`.
|
|
|
|
|
|
|
|
|
|
## 6.3 - Control the outputed field
|
|
## 6.3 - Control the output field
|
|
|
|
|
|
One can specify which fields to output by adding a `fields` attribute or an `exclude` attribute in the `Meta` class. The default is to output all fields (`fields = '__all__'`).
|
|
One can specify which fields to output by adding a `fields` attribute or an `exclude` attribute in the `Meta` class. The default is to output all fields (`fields = '__all__'`).
|
|
|
|
|
... | | ... | |