python - Dynamically creating models in Django -
i want create many models based on abstract one, different default values fields.
def get_model(name): class abstractmodel(models.model): field1 = models.charfield(max_length=100, default=name) class meta: abstract = true return abstractmodel class mymodel(get_model('mymodel')): pass
what disadvantages of approach presented above? there better way?
it looks need single model instead of set of models. might have single model additional field having kind of model type - same model name.
there lots of disadvantages of idea. confusing others , influence negatively performance due constant using code reflections.
update
default value name dependent on class can make overwriting in constructor:
from django.db import models class a(models.model): def __init__(self): self.f = 'model name' f = models.charfield(max_length=100) class b(a): def __init__(self): self.f = 'model b name'
then try in interactive session:
>>> polls import models >>> models.a().f 'model name' >>> models.b().f 'model b name'
Comments
Post a Comment