I am trying to create a framework using Django rest API. I want to configure Serializer fields and Name of class variable of serializer dynamically using a factory pattern. My code is a bit complex so here is the simplified version of problem.
from rest_framework import serializers
class FixedSrializer(serializers.Serializer):
email= serializers.EmailField()
fixed_serializer=FixedSrializer(data={"email":"invalidemail"})
fixed_serializer.errors
#OUTPUT: {'email': [u'Enter a valid email address.']}
# Trying Above by dynamically assigning class var
class CustomSrializer(serializers.Serializer):
pass # no class var assigned
#setattr(CustomSrializer,"email",serializers.EmailField())
CustomSrializer.email=serializers.EmailField()
custom_serializer=CustomSrializer(data={"email":"invalidemail"})
custom_serializer.errors
#OUTPUT# {}
#EXPECTED# {'email': [u'Enter a valid email address.']}
I think this may be because of dynamically changing class variable. Please correct where I am doing wrong.