Added presentation

This commit is contained in:
2025-03-13 22:29:13 +03:30
parent 7dfe29b10d
commit 74ff118612
2 changed files with 15 additions and 2 deletions

View File

@@ -48,10 +48,23 @@ class BaseModel( models.Model ):
await sync_to_async(lambda: [ getattr(self, f) for f in fields ])()
def fields_to_dict( self, *fields ):
_model_dict_presentations = {}
@classmethod
def dict_presentation( cls, name ):
def attacher_decorator( f ):
cls._model_dict_presentations[name] = f
return f
return attacher_decorator
def fields_to_dict( self, *fields, presentation_name = None ):
"""
To create json/dict from fields.
"""
if presentation_name:
assert presentation in self._model_dict_presentations, f"This model '{self.__class__}' does not have a '{presentation_name}' presentation!"
fields.extend( self._model_dict_presentations[presentation_name] )
results = {}
for f in fields:
name, method = (f[0], f[1]) if isinstance(f, tuple) else (f, f)