简体   繁体   中英

Load data from model in settings.py Django

I have model:

class Settings(models.Model):
    class Meta:
        verbose_name = "Ustawienia strony"
        verbose_name_plural = "Ustawienia strony"

    title = models.CharField(_('Tytuł strony'), max_length = 120, blank = True)
    description = models.TextField(_('Opis strony'), blank = True)

    facebook_title = models.CharField(_('Tytuł strony Open Graph'), max_length = 120, blank = True,)
    facebook_type = models.CharField(_('Typ strony Open Graph'), max_length = 120, blank = True,)
    facebook_url = models.CharField(_('Link strony Open Graph'), max_length = 500, blank = True,)
    facebook_description = models.TextField(_('Opis strony Open Graph'), blank = True, )

    template = models.CharField(_("Wygląd sklepu"), choices = TEMPLATES_LIST, max_length = 100, default="DEFAULT")

Now I would like load selected model in settings.py:

CURRENT_TEMPLATE = Settings.objects.all().first()

But in this way I have error: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Is it available to load data from model in settings.py?

Did you put CURRENT_TEMPLATE in setting.py? Apps must be loaded before importing models. Here is the solution, use a Constants class for storing your CURRENT_TEMPLATE, in your apps.py:

from django.apps import AppConfig


class YourAppConfig(AppConfig):
    name = 'yourapp'

    def ready(self):
        # import Setting
        # Constants.CURRENT_TEMPLATE = Setting.objects.all().first()

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