I'm using django with many related projects. All those projects are using 
their own models. But unfortunately this is not DRY, because each model 
differ from each other very slightly, for example some columns or tables 
from one model are missing in other, or some model defines custom save 
method while the other one does not. 

My idea was to define simple core model - the one that has all tables and 
columns and no custom methods.

Then I could dynamically create other models using code like this:



    def createModelClass(modelClass):
        dict = {}
        for field in modelClass._meta.fields:
             if modelClass.__name__ not in EXCLUDED_FIELDS or field.name 
not in EXCLUDED_FIELDS[modelClass.__name__]:
                dict[field.name] = field

        dict["Meta"] = type('Meta', (ChemblCoreAbstractModel.Meta, object), 
{'unique_together': modelClass._meta.unique_together})
        dict["__module__"] = mod.__name__

        return type(modelClass.__name__, (CoreAbstractModel, ), dict)

    #------------------------------------------------------

    def getCoreModelClasses(module):
        return  [x[1] for x in 
inspect.getmembers(sys.modules[module.__name__]) if
                 inspect.isclass(x[1]) and issubclass(x[1], 
CoreAbstractModel) and x[1].__name__ not in EXCLUDED_MODELS]

Unfortunately I can't do this because fields remember their models 
(especially related fields). So in principle I would have to change the 
code to create new fields from scratch just reading attributes from the 
original model. 

This makes no sense: inspect model to create another model. In that case 
would be much better to create model from some XML specification that would 
be easier to read and modify and which is not bound to any particular 
django app. 

So my question is - is there any django plugin that allows to express model 
in terms of xml and then create app specific model from this xml?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to