[Fixed]-Django Rest Framework override model fields in modelserialzer


You forgot to add get_ prefix. Your methods names should have get_ prefix as it is mentioned in SerializerMethodField docs

def get_offer(self, obj):
    return 123

def get_cart_item(self, obj):
    return 123

def get_stock_details(self, obj):
    return 123


It is also possible to pass the method name as an optional parameter to each serializers.SerializerMethodField() and specify the custom method name you want to use for each custom/overridden field. This way you can omit the get_ prefix in the resolver method names inside the serializer class.

Leave a comment