SIMILAR_MODELS_TEMPLATE=
"""
@some_decorator
class %(PREFIX)sModel (Some_Abs_model):
\"""%(DESCRIPTION)s
\"""
pass
"""
def similar_models_factory(prefix,description):
format_dict = {
'PREFIX' : prefix,
'DESCRIPTION' : description,
}
cls_name = "%sModel" % prefix
cls_source = SIMILAR_MODELS_TEMPLATE % format_dict
exec(cls_source)
new_cls = locals().get(cls_name)
return new_cls
from my_factories import similar_models_factory
SIMILAR_MODELS_LIST =(
{'prefix': 'Foo', 'desc' : 'Foo model and etc..',},
{'prefix': 'Bars', 'desc' : 'Bars model and etc..',},
.....
)
for smodel in SIMILAR_MODELS_LIST:
cls = similar_models_factory(smodels['prefix'], smodels['desc'])
vars()[cls.__name__] = cls
exec ""
>>> class X(object): ... a = 1 Is exactly the same as doing... >>> X = type('X', (object,), dict(a=1))No exec required...
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/64JCLnsIFu0J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
-- Philip Mountifield Formac Electronics Ltd tel +44 (0) 1225 837333 fax +44 (0) 1225 430995 pmount...@formac.net www.formac.net www.telgas.net
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
-- Philip Mountifield Formac Electronics Ltd tel +44 (0) 1225 837333 fax +44 (0) 1225 430995 pmount...@formac.net www.formac.net www.telgas.net
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/pxR15NDv5RUJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.