Anmol Monga
Anmol Monga

Reputation: 321

how to add dynamic fields at run time in django

I have to add dynamic fields at run time in my django application,but I don't know the proper way how to add new fields at run time.

I want to add the code which will generate the dynamic field and will update database too. I am using postgresql database. please help if anyone can.

My "model.py" is simply like this:

class Student(models.Model):

    name=models.CharField(max_length=100)
    school=models.CharField(max_length=100)
    created_at=models.DateField(auto_now_add=True)  
    is_active=models.BooleanField(default=False)


    def __str__(self):
        return self.name

Upvotes: 6

Views: 2593

Answers (1)

Klaus D.
Klaus D.

Reputation: 14369

Django is not made for dynamic models, as relational databases are not. A model change at runtime will create a ton of problems.

You have to simulate it, by...

  • clever use of related models
  • storing values in a large field, e.g. JSON as text
  • having a generic model that stores the data as key, value; e.g. a table with PK, a FK, key, value as columns.

You should try the first option and only if that does not work out try the other two.

Upvotes: 2

Related Questions