Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 956
Anmol Monga Avatar asked Oct 30 '22 18:10

Anmol Monga


1 Answers

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.

like image 169
Klaus D. Avatar answered Nov 15 '22 04:11

Klaus D.