Last active
July 15, 2023 11:20
-
-
Save rg3915/6c094fbff9417f7b5c7084e143675f52 to your computer and use it in GitHub Desktop.
model serializable_value to_dict Django - Como gerar um dicionário serializável de um objeto automaticamente com serializable_value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyModel(models.Model): | |
# Como gerar um dicionário serializável de um objeto automaticamente com serializable_value | |
# https://stackoverflow.com/a/39384659/802542 | |
... | |
def to_dict(self, exclude=[]): | |
# https://stackoverflow.com/a/39384659/802542 | |
tree = {} | |
for field in self._meta.fields: | |
if field.name in exclude: | |
continue | |
tree[field.name] = self.serializable_value(field.name) | |
return tree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment