How To Dynamically Set Attributes To A Class In Models In Django?
I want to dynamically set attribute to models. Here is the way I did it. # models.py class pwr(models.Model): # test info tester = models.CharField(max_length=10) te
Solution 1:
You cannot set it dynamically with setattr
because you are dealing with Python descriptors, not attributes. This is because of the order of events when the module is imported and Python class ans descriptors put together.
Naturally, you can still do this in superious dynamic language like Python. But the solution for the problem needs different approach
You can use Python metaclasses and override
__new__
See declared_attr in SQLAlchemy, its own way to solve this special case
Post a Comment for "How To Dynamically Set Attributes To A Class In Models In Django?"