Skip to content

Instantly share code, notes, and snippets.

@ragne
Created April 14, 2017 22:54
Show Gist options
  • Save ragne/0d8831b55dd0d7e0092d724659091441 to your computer and use it in GitHub Desktop.
Save ragne/0d8831b55dd0d7e0092d724659091441 to your computer and use it in GitHub Desktop.
def auto_props(cls):
gm2m_fields = map( lambda x: x[0],
filter(lambda x: isinstance(x[1], GM2MField), cls._meta._forward_fields_map.items()))
for field in gm2m_fields:
for model in getattr(cls, field).get_related_models(include_auto=True):
model_lower = model.lower()
if not hasattr(cls, model_lower):
setattr(cls, model_lower, property(lambda self:
map(lambda x: x.gm2m_tgt,
self.settings.filter(Model='.'.join([cls._meta.app_label, model])))))
return cls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment