Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

python - Re-evaluate class OR make meta inner class lazy

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 globalvariablenamevalue

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...