Django model db_table inheritance/injection -
i'm doing integration between django , aws redshift reporting purposes. far feels django isn't doing it. 1 of problems encountered have set alternative type id each model, rather autofield type because it's not supported in redshift. far good, thought if i'm doing super class models i'm going use change table names on way, don't default names django gives it's models in db (app_name__model_name). managed every sub model (see below). declare dynamically in basemodel in way each sub model receive it's own table name according it's class name.
class basemodel(model): id = charfield(primary_key=true, max_length=36) class meta: abstract = true def __init__(self, *args, **kwargs): super(redshiftbasemodel, self).__init__(*args, **kwargs) self.id = str(uuid4()) class submodel1(basemodel): class meta(basemode.meta): db_table = 'submodel1' field1 = charfield(max_length=64, primary_key=true) class submodel2(basemodel): class meta(basemode.meta): db_table = 'submodel2' field2 = charfield(max_length=64, primary_key=true)
is such thing possible?
firstly, certainly not want set self.id
in __init__
method; called every time item instantiated, when being loaded database, in case db id overridden. better make default value:
id = charfield(primary_key=true, max_length=36, default=uuid4)
(although note django 1.8 introduced uuidfield, use that).
to answer main question, though, possible - through complex overriding of model metaclass (not confused model's meta class...). more trouble it's worth.
Comments
Post a Comment