从settings.py中的模型加载数据

时间:2019-08-15 16:31:42

标签: python django

我有模特:

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")

现在,我想在settings.py中加载所选模型:

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

但是这样我有错误: django.core.exceptions.AppRegistryNotReady:应用尚未加载。

是否可以从settings.py中的模型加载数据?

1 个答案:

答案 0 :(得分:0)

您是否将CURRENT_TEMPLATE放在setting.py中?在导入模型之前,必须先加载应用。 这是解决方案,使用Constants类在您的apps.py中存储CURRENT_TEMPLATE:

from django.apps import AppConfig


class YourAppConfig(AppConfig):
    name = 'yourapp'

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