I genuinely can't figure this out - been working on it for days.
In django I have:
globalvariablename = "tablename"
class foo (models.Model):
# Some attributes I don't care about
class Meta:
db_table = globalvariablename
Now it's pretty obvious both foo and Meta will be evaluated once the class is imported / server is instantiated.
Problem: I want db_table
to be reassigned once I change globalvariablename
value
My question:
- Is there anyway to make the Meta inner class lazy i.e. for it not to be evaluated until the model is used (e.g. when data is fetched from the database)?
- Alternatively, is there any way to re-evaluate Meta's
db_table
attribute once the globalvariablename
has changed?
Any help is appreciated. I'm literally pulling my hair out
I've experimented lots with decorators i.e. for the Meta inner class to be added dynamically. This doesn't work because db_table
is already assigned automatically as soon as the model is created and decorating it will just create additional duplicate values of db_table.
question from:
https://stackoverflow.com/questions/65891200/re-evaluate-class-or-make-meta-inner-class-lazy 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…