Skip to content

Instantly share code, notes, and snippets.

@rg3915
Last active July 15, 2023 11:20
Show Gist options
  • Save rg3915/6c094fbff9417f7b5c7084e143675f52 to your computer and use it in GitHub Desktop.
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
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