You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.
The current easiest way to do this is through tagging, and then overriding the from_dict and to_dict methods of the Abstract model to serialize and deserialize the tag field.
classAbstract(Model):
x=fields.Str()
@classmethoddeffrom_dict(cls, d, **kwargs):
ifclsisAbstract: # this is the key part that makes this all worktag=d.pop('tag')
subcls= {c.__name__: cforcincls.__subclasses__()}[tag]
returnsubcls.from_dict(d, **kwargs)
returnsuper(Abstract, cls).from_dict(d, **kwargs)
defto_dict(self, **kwargs):
d=self.to_dict(**kwargs)
d['tag'] =self.__class__.__name__returnd
You can even create a subclass-able Tagged Model.
classTagged(Model):
@classmethoddeffrom_dict(cls, d, **kwargs):
ifTaggedincls.__bases__: # this is the key part that makes this all worktag=d.pop('tag')
subcls= {c.__name__: cforcincls.__subclasses__()}[tag]
returnsubcls.from_dict(d, **kwargs)
returnsuper(Tagged, cls).from_dict(d, **kwargs)
defto_dict(self, **kwargs):
d=self.to_dict(**kwargs)
d['tag'] =self.__class__.__name__returnd
But it might be better to do this with decorators? We would also want to have the tag field be configurable. Maybe this is the time to add a Meta inner class, that can be used by the ModelType metaclass to adjust Model settings.
Perhaps we should move away from metaclasses completely and use decorators like attrs.
The text was updated successfully, but these errors were encountered:
It is very common to want to have an abstract Model that can will be automatically deserialized into the correct subclass.
Example
The current easiest way to do this is through tagging, and then overriding the
from_dict
andto_dict
methods of the Abstract model to serialize and deserialize the tag field.You can even create a subclass-able
Tagged
Model.But it might be better to do this with decorators? We would also want to have the
tag
field be configurable. Maybe this is the time to add aMeta
inner class, that can be used by theModelType
metaclass to adjust Model settings.Perhaps we should move away from metaclasses completely and use decorators like
attrs
.The text was updated successfully, but these errors were encountered: