Skip to content

Instantly share code, notes, and snippets.

@nicksnell
Created February 25, 2014 10:16
Show Gist options
  • Select an option

  • Save nicksnell/9206327 to your computer and use it in GitHub Desktop.

Select an option

Save nicksnell/9206327 to your computer and use it in GitHub Desktop.
Utility to pull all the callable methods from a given Django model. Tries to ignore default internal methods.
import yaml
DEFAULT_IGNORE = [
'__unicode__', '__module__', '_meta', 'MultipleObjectsReturned', '_base_manager',
'objects', 'DoesNotExist', '__doc__', '_default_manager', '_curried',
]
def analyze_model(model, ignore=[], default_ignore=True):
"""Analyze a Django Model to return a list of methods along with
the methods docstring"""
if default_ignore:
ignore.extend(DEFAULT_IGNORE)
model_keys = model.__dict__.keys()
methods = [key for key in model_keys if not key in ignore]
results = {}
for method in methods:
try:
model_method = getattr(model, method)
except AttributeError:
continue
if callable(model_method) and not model_method.__name__ == '_curried':
results[method] = model_method.__doc__
return yaml.dump({model.__name__: results}, default_flow_style=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment