Home > Enterprise >  Django create db table headers from dict
Django create db table headers from dict

Time:09-22

I am trying to create a model.Model with the columnheaders(key) and types(value). I tried it with the setattr(self,columnheader,columntype) but this seems to not work. Can i dynamically alter the table with django inbuilts or do i have to use raw sql?

Example dict:

_dict = { "column_1": models.IntegerField(),
"column_2": models.IntegerField()
}

CodePudding user response:

You can assign this to the local variables, but I would advise against this:

_dict = {
    'column_1': models.IntegerField(),
    'column_2': models.IntegerField()
}

class MyModel(models.Model):
    locals().update(_dict)
  • Related