#17143: Update docs for nested serializer use

This commit is contained in:
Jeremy Stretch
2024-08-30 10:18:29 -04:00
parent a6b1f97525
commit 10a74492f6
4 changed files with 7 additions and 30 deletions

View File

@@ -25,6 +25,8 @@ project-name/
Serializers are responsible for converting Python objects to JSON data suitable for conveying to consumers, and vice versa. NetBox provides the `NetBoxModelSerializer` class for use by plugins to handle the assignment of tags and custom field data. (These features can also be included ad hoc via the `CustomFieldModelSerializer` and `TaggableModelSerializer` classes.)
The default nested representation of an object is defined by the `brief_fields` attributes under the serializer's `Meta` class. (Older versions of NetBox required the definition of a separate nested serializer.)
#### Example
To create a serializer for a plugin model, subclass `NetBoxModelSerializer` in `api/serializers.py`. Specify the model class and the fields to include within the serializer's `Meta` class.
@@ -41,31 +43,7 @@ class MyModelSerializer(NetBoxModelSerializer):
class Meta:
model = MyModel
fields = ('id', 'foo', 'bar', 'baz')
```
### Nested Serializers
There are two cases where it is generally desirable to show only a minimal representation of an object:
1. When displaying an object related to the one being viewed (for example, the region to which a site is assigned)
2. Listing many objects using "brief" mode
To accommodate these, it is recommended to create nested serializers accompanying the "full" serializer for each model. NetBox provides the `WritableNestedSerializer` class for just this purpose. This class accepts a primary key value on write, but displays an object representation for read requests. It also includes a read-only `display` attribute which conveys the string representation of the object.
#### Example
```python
# api/serializers.py
from rest_framework import serializers
from netbox.api.serializers import WritableNestedSerializer
from my_plugin.models import MyModel
class NestedMyModelSerializer(WritableNestedSerializer):
foo = SiteSerializer(nested=True, allow_null=True)
class Meta:
model = MyModel
fields = ('id', 'display', 'foo')
brief_fields = ('id', 'url', 'display', 'bar')
```
## Viewsets