Skip to content

Instantly share code, notes, and snippets.

@IsaacHatilima
Created January 23, 2021 16:39
Show Gist options
  • Save IsaacHatilima/990ea742f8e4bc9015df352b0a76fbe1 to your computer and use it in GitHub Desktop.
Save IsaacHatilima/990ea742f8e4bc9015df352b0a76fbe1 to your computer and use it in GitHub Desktop.
Nested Django Rest Framework Sterilizers
from rest_framework import serializers
from authentication.models import *
from authentication.serializers import RegisterSerializer
class UserSerializer(serializers.ModelSerializer):
class Meta:
#Notice there is no fields i the Meta class
model = User
exclude = ('password', 'groups','user_permissions')
extra_kwargs = {
'password': {'write_only': True},
'another_field': {'read_only': True},
'username': {'read_only': True},
'email': {'read_only': True}
#add other fields
}
class ProvinceDetailedSerializer(serializers.ModelSerializer):
#make sure you add required=False in the UserSerializer so that you don't get error of invalid created_by_id or whatever you user FK is
created_by = UserSerializer(read_only=True,required=False)
class Meta:
model=Province
fields = ('id','province_name','created_by','created_at')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment