get_serializer_class not working on production server

40 views
Skip to first unread message

Sven Mäurer

unread,
Apr 1, 2015, 9:31:40 AM4/1/15
to django-res...@googlegroups.com
In the ViewSets I want to use get_serializer_class to return the right serialiser for create and retrieve. In development mode it is working but on my production system it doesn't.

def get_serializer_class(self):
if self.action is 'create':
return AddBuildingSerializer
elif self.action is 'retrieve':
return RetrieveBuildingSerializer
else:
return BuildingSerializer

Ion Scerbatiuc

unread,
Apr 1, 2015, 8:16:09 PM4/1/15
to django-res...@googlegroups.com
Hey,

You should not use 'is' for equality checking, but rather '=='. Is will check that the variable on the left side is the exact same object in memory as the one on the right side (in this case a string constant). This might work on the local box, but it's unpredictable. The string constant 'create' (or 'retrieve') can definitely have a different memory location than `self.action`.

-Ion

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sven Mäurer

unread,
Apr 2, 2015, 4:12:38 AM4/2/15
to django-res...@googlegroups.com
Thank you so much. Also for the long explanation. I think I have to read in a little bit more about Python, as a elsewise Java developer ;-)
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages