Serialization and Polymorphism
I've been recently working on the comment posting module of Vialogues. One of my interests is the serialization method used for passing complicated objects between the UI and backend. Objects can exist in three forms, normal, serialized and stringified. Serialization converts an object to a dictionary structure, which is reliable for communication.
An issue about serialization arose today. Three classes were derived from the same super class and they have different serialization methods. The requirement is to just use the super class model to fetch derived classes' objects from db and serialize them in their specific way. This is purely about polymorphism. The Django framework is good at addressing but doesn't work naturally well on. After consulting online, a utility was found which provides InheritanceCastModel
, helping to call specific methods for each subclass. The model implements polymorphism by adding a type field to objects and cast them to their real class when they are fetched under the super class model.