0

我想要導入0123.zg中PUSH_NOTIFICATIONS_SETTINGS的setting.py中的應用程序模型。這是我的代碼:Django設置 - 應用程序尚未加載

INSTALLED_APPS = (
    .... 
    'my_app', 
    'push_notifications' 
    .... 
) 

from my_app.models import User 

PUSH_NOTIFICATIONS_SETTINGS = { 
    'GCM_API_KEY': 'xxxxx', 
    'APNS_CERTIFICATE': 'xxxxx.pem', 
    'USER_MODEL': User, # i want to change the default from auth_user to my_app User 
} 

但它在這一行引發錯誤:

from my_app.models import User 

的錯誤是:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. 

我怎樣才能加載程序my_app模型setting.py?

回答

1

您無法像這樣從設置文件中加載模型 - 模型只能在加載所有應用程序後加載(只能在加載設置後才能加載模型)。

綜觀django-push-notifications的代碼,你應該能夠提供模型與虛線路徑的字符串:

'USER_MODEL': 'my_app.User' 
1

不能加載設置用戶模型,而是你可以改變它

PUSH_NOTIFICATIONS_SETTINGS = { 
    'GCM_API_KEY': 'xxxxx', 
    'APNS_CERTIFICATE': 'xxxxx.pem', 
    'USER_MODEL': 'my_app.User', 
} 

而且使用後,如:

from django.apps import apps 
from django.conf import settings 
User = apps.get_model(settings.PUSH_NOTIFICATIONS_SETTINGS['USER_MODEL']) 

你可以做w ^你恨這個用戶模式的憎恨者

相關問題