6

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
Wtower
  • 18,848
  • 11
  • 103
  • 80
Anmol Monga
  • 311
  • 2
  • 15
  • 2
    suggestion- use `division` as column name. Do not use `class` as column name because `class` keyword have its own properties. – Vivek Sable Oct 01 '15 at 09:16
  • Thank you for suggestion. I changed it. – Anmol Monga Oct 01 '15 at 09:19
  • Can you tell what is the use case for dynamic fields ? – Raja Simon Oct 01 '15 at 09:26
  • Actually I am trying to develop a application for colleges. I will add all necessary fields for student in model.py but if in case college admin want an attribute according to their need, e.g. their own college_id or any other, then what? So that's why I want option at runtime to add new fields. – Anmol Monga Oct 01 '15 at 09:36
  • You have a requirement, lets say some students to have attributes that others do not have and are tons of possibilities for that? If so take a look at NoSQL (Document stores, column stores, ...) the schema constraints there are more loose. Some guidance: [Django Mongo](https://django-mongodb-engine.readthedocs.org/en/latest/), or [How to use Cassandra in Django framework](http://stackoverflow.com/questions/2369793/how-to-use-cassandra-in-django-framework) Hope this helps. – jlnabais Oct 01 '15 at 09:37
  • Thank you sir........... – Anmol Monga Oct 01 '15 at 11:48

1 Answers1

2

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.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
  • He can also make use of NoSQL (document stores, column stores,...). However, I haven't understand what are the dynamic requirements of the application in this case. – jlnabais Oct 01 '15 at 09:28
  • Actually I am trying to develop a application for college database. I will add all necessary fields related to college database but if in case college admin want an attribute according to their need, e.g. their own college_id or any other, then what? So that's why I want option at runtime to add new fields according to college admin. Is there any other solution???? – Anmol Monga Oct 01 '15 at 09:40
  • Don't do that. They will mess around with it and the database and it's data will become impossible to maintain after a short time. If there is a need for an additional field, they should file a change request to you. With form models and all the other fine features in Django you can add it quickly. And you can check it the field is clever, fit the application and is not added somewhere else already. – Klaus D. Oct 01 '15 at 10:59