I'm doing something like this to generate some models dynamically:

SIMILAR_MODELS_TEMPLATE=
> """
> @some_decorator
> class %(PREFIX)sModel (Some_Abs_model):
>     \"""%(DESCRIPTION)s
>     \"""
>     pass
> """


then I have a factory:

> 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


And finally I have in the models.py:

> 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


And this gives my just what I want, but I read somewhere that if you do:

> exec ""

 
It would be too costly, and I would like to know if this would have some 
heavy load in my project(and if please, how can I calculate this, to 
compare?) 
Thanks!

-- 
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-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to