Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.