I have two models (User and Province) and serializers. The user creates a province and in the province model I have the user id. The challenge is when creating nested serializers, I only want the first_name and last_name from the user but is is giving me everything including the password. Below are the serializers
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['first_name', 'last_name']
class ProvinceSerializer(serializers.ModelSerializer):
user = UserSerializer(many=True, read_only=True)
class Meta:
model = Province
fields = ['id', 'province_uid', 'province_name', 'date_created','created_by','user' ]
read_only_fields = ('id','province_uid', 'date_created')
depth = 1
Edit Below is the response and I would like to only have the first_name and last_name from the user.
"id": 1,
"province_uid": "9c74aeda-0734-465f-847b-0fa89ca7ea21",
"province_name": "Lusaka",
"date_created": "2021-07-12T06:42:22.973441+02:00",
"created_by": {
"id": 1,
"password": "db_password",
"user_uid": "3d0f353c-260a-41b7-a535-3ea5a20fe8a5",
"username": "admin",
"email": "[email protected]",
"cell": "260966477311",
"first_name": "Global",
"last_name": "Admin"}
depth=1
? I assume it overrides yourfields=[....,'user']
– Klim BimProvinceSerializer
, changeuser
tocreated_by
– BrianD