Django create db table headers from dict


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()
Asked By: Mole



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):
Answered By: Willem Van Onsem
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.