Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
Created February 7, 2013 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdamusic/4734319 to your computer and use it in GitHub Desktop.
Save lambdamusic/4734319 to your computer and use it in GitHub Desktop.
Django: Django: access the attributes of a model dynamically
def attrs_verbose(self):
model = self.__class__
# using this form: Record._meta.get_field('created_by').verbose_name
items = []
for k, v in self.__dict__.items():
try:
x = model._meta.get_field(k).verbose_name
except:
x = k
items += [(x, v)]
items.sort()
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment