我想在django-push-notifications中的PUSH_NOTIFICATIONS_SETTINGS的setting.py中导入应用模型。这是我的代码:
#include "appl.h"
using namespace std;
/*
Class definition file for Appl
*/
string getFileName(ios_base::open_mode parm);
struct wordBlock {
string word;
int count;
};
// Constructor
Appl::Appl()
{
// id string is required for all CS 162 submissions. *** DO NOT CHANGE ***
_cs162_id_ = new string(__FILE__ + string(" compiled ")
+ __DATE__ + string(" ") + __TIME__
+ string(" using g++ ") + to_string(__GNUC__)
+ string(".") + to_string(__GNUC_MINOR__) + string(".")
+ to_string(__GNUC_PATCHLEVEL__));
}
// Destructor
Appl::~Appl()
{
delete _cs162_id_;
}
string inputFileName(ios_base::open_mode parm){
fstream iFile;
string inFileName = "";
int count = 1;
if(parm == ios::in){
while(count != 0){
cout << "Enter an input file name that exists: ";
getline(cin,inFileName);
iFile.open(inFileName.c_str() , ios::in);
if(iFile.good() != true){
cout << "?Invalid file name : file does not exist" <<
count++;
iFile.clear();
}else{
count = 0;
iFile.close();
return inFileName;
}
}
}
}
// Main Routine
int Appl::main(int argc, char ** argv)
{
fstream inFile;
string inFileNames;
inFileNames = inputFileName(ios::in);
inFile.open(inFileNames.c_str(), ios::in);
wordBlock inventory[1000];
if(inFile.is_open()){
for(auto idx = 0; idx < 1000; idx ++){
inventory[idx].word = idx;
inventory[idx].count = 0;
}
while(inFile.peek() != EOF) {
inventory[inFile.get()].count++;
}
for(auto idx = 0; idx < 1000; idx++){
inventory[idx].word = idx;
inventory[idx].count = 0;
}
while(inFile.peek() != EOF) {
inventory[inFile.get()].count++;
}
for(auto idx = 0; idx < 1000; idx++){
if(inventory[idx].count == 0) continue;
cout << "Word " << inventory[idx].word << " occurs " << inventory[idx].count << " times" << endl;
}
inFile.clear();
inFile.close();
}
return 0;
}
但它在这一行引起了错误:
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
如何在setting.py中加载my_app模型?
答案 0 :(得分:1)
您无法在设置文件中加载模型 - 模型只能在加载所有应用程序后加载(只有在加载设置后才能加载)。
查看django-push-notifications
中的代码,您应该能够将模型提供为带有虚线路径的字符串:
'USER_MODEL': 'my_app.User'
答案 1 :(得分: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'])
您可以使用此用户模型做任何您想做的事情