简体   繁体   中英

How to add a dynamic value in settings.py Django

I want to maintain a variable TOKEN in my settings.py of my django project, and it changed with time.

The token saved at db like:

class FbBmToken(models.Model):
    manager_name = models.CharField(max_length=64)
    token = models.CharField(max_length=255, blank=True, null=True)
    effective = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'fb_bm_token'

And I want use it in my django project anywhere by

from django.conf import settings


token = settings.TOKEN

How can I make this happen?

Greate thanks

I think you should create a method to get the latest token from your database. you should try this:

def get_latest_token():
   token = FbBmToken.objects.latest('id')
   token.refresh_from_db()
   return token

Now, you can directly call this method whenever you want to get the latest updated token. I hope this would work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM