Check https://github.com/JeffHeard/ga_dynamic_models. It may be similar to what you're looking for. Even if you don't want the code, you can see a way of dynamically generating models.
On Mon, Apr 16, 2012 at 12:40 PM, Philip Mountifield < pmountifi...@formac.net> wrote: > Did you know that you can use the type function to dynamiclly generate a > class definition? > > See http://docs.python.org/library/functions.html?highlight=type#type > > From the example: > > >>> class X(object):... a = 1 > Is exactly the same as doing...>>> X = type('X', (object,), dict(a=1)) > > No exec required... > > Regards > Phil > > > > On 16/04/2012 17:19, Arruda wrote: > > 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. > > > > -- > > Philip Mountifield > Formac Electronics Ltd > tel +44 (0) 1225 837333 > fax +44 (0) 1225 430995 > pmountifi...@formac.netwww.formac.netwww.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-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. > -- You received this message because you are subscribed to the Google Groups "Django users" group. 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.