-
Notifications
You must be signed in to change notification settings - Fork 81
Serializers
core.serializers.DetailSerializer
allows you to create compact representations on your model instances, by excluding fields from serialization. This can be useful when serializing a compact list of model instances and only provide detailed information when accessing a single instance.
DetailSerializer
automatically creates compact representations whenever you attempt to serialize a list of model instances. To force detailed serialization with lists, add detail=True
as a keyword argument when initializing the serializer.
To use DetailSerializer
, add it to the list of base classes, usually it goes next the Rest Framework's ModelSerializer
. To exclude fields from the compact view, add the field name to the detail_only_fields
attribute in the serializer's Meta
class.
from core.serializers import DetailSerializer
from rest_framework.serializers import ModelSerializer
class OrganizationSerializer(DetailSerializer, ModelSerializer):
class Meta:
model = Organization
fields = ('id', 'slug', 'name', 'description', 'archived', 'urls',
'contacts', 'users')
read_only_fields = ('id',)
detail_only_fields = ('users',)
Instantiate the serializer normally:
org = some_list_of_organizations;
s = OrganizationSerializer(org)
s.data # returns organizations without users, if org is a list.
For detailed representations with lists:
org = some_list_of_organizations;
s = OrganizationSerializer(org, detail=True)
s.data # returns organizations including users
Visit our User Documentation to learn more about using the Cadasta Platform.
If you'd like to contribute to the Cadasta Platform, start with our Contributing Guidelines.
Cadasta Wiki Home | Developer Setup Guide
Cadasta.org | About Cadasta | YouTube | Twitter | Facebook
- Installing & Running
- Contributing
- Planning & Sprints
- Platform Development
- Testing
- Utilities
- Outreachy
- Platform Site Map
- User Flows and Wireframes
- Other
- Quick Start Guide
- Glossary
- Questionnaire Guide