12👍
You can use SerializerMethodField
:
class EmployeeSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField()
ssn = SerializerMethodField()
class Meta:
model = Employee
fields = ('id','ssn')
read_only_fields = ['id']
def get_ssn(self, obj):
return '***-**-{}'.format(obj.ssn.split('-')[-1]
6👍
If you don’t need to update the ssn, just shadow the field with a SerializerMethodField
and define get_ssn(self, obj)
on the serializer.
Otherwise, the most straightforward way is probably to just override .to_representation()
:
def to_representation(self, obj):
data = super(EmployeeSerializer, self).to_representation(obj)
data['ssn'] = self.mask_ssn(data['ssn'])
return data
Please add special case handling ('ssn' in data
) as necessary.
- Django newbie deployment question – ImportError: Could not import settings 'settings'
- Django session expiry?
- How to make GET CORS request with authorization header
- Django Rest Framework override model fields in modelserialzer
- How to use pytest fixtures with django TestCase
2👍
Elaborating on @dhke’s answer, if you want to be able to reuse this logic to modify serialization across multiple serializers, you can write your own field and use that as a field in your serializer, such as:
from rest_framework import serializers
from rest_framework.fields import CharField
from utils import mask_ssn
class SsnField(CharField):
def to_representation(self, obj):
val = super().to_representation(obj)
return mask_ssn(val) if val else val
class EmployeeSerializer(serializers.ModelSerializer):
ssn = SsnField()
class Meta:
model = Employee
fields = ('id', 'ssn')
read_only_fields = ['id']
You can also extend other fields like rest_framework.fields.ImageField to customize how image URLs are serialized (which can be nice if you’re using an image CDN on top of your images that lets you apply transformations to the images).
- You have 3 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth
- How to serve Django for an Electron app
- Provide tab title with reportlab generated pdf
- How to redirect from a view to another view In Django