how to add dynamic fields at run time in django

对着背影说爱祢 提交于 2021-02-08 15:05:55

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/32883759/how-to-add-dynamic-fields-at-run-time-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!