0

我想爲在Django的syncdb(執行後)創建超級用戶定義一個固定的用戶名和密碼。我在下面使用的方法是在較舊版本的Django(我猜1.6)中工作,但現在它不工作。Django超級用戶夾具錯誤 - 沒有這樣的表:auth_user

我有這個fixture文件initial_data.json

[ 
{ 
    "fields": { 
    "username": "me", 
    "first_name": "", 
    "last_name": "", 
    "is_active": true, 
    "is_superuser": true, 
    "is_staff": true, 
    "last_login": null, 
    "groups": [], 
    "user_permissions": [], 
    "password": "pbkdf2_sha256$...", 
    "email": "[email protected]", 
    "date_joined": "2015-04-23T01:13:43.233Z" 
    }, 
    "model": "auth.user", 
    "pk": 1 
} 
] 

當我添加這settings.INSTALLED_APPS

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'my_app', 

) 

和運行python manage.py syncdb,我得到以下錯誤:

django.db.utils.OperationalError: Problem installing fixture 'path/initial_data.json': Could not load auth.User(pk=1): no such table: auth_user 

什麼我應該怎麼做?

我可以更改夾具加載的順序以確保在加載夾具之前創建了auth_user表嗎?

或者任何其他方式來自動創建一個超級用戶在Django?

在此先感謝。

+0

是否將'auth.user'更改爲'auth.User'幫助? –

+0

它抱怨你的數據庫沒有auth_user表。你能打開你的數據庫並檢查該表是否在那裏? – Cheng

+0

@Cheng我通過手動刪除my_app,syncdb和創建超級用戶來獲得這個json文件。所以是的,它創建了表格,但是如果我包含這個燈具,它就會失敗。 – jeff

回答

1

我解決了我的問題,但它絕對不是最漂亮的解決方案。

我看到,當我運行heroku run python manage.py syncdb,我得到以下

Operations to perform: 
    Synchronize unmigrated apps: myproject, permissions, myapp1, myapp2 
    Apply all migrations: auth, flatpages, admin, contenttypes, sessions, sites 
Synchronizing apps without migrations: 
    Creating tables... 
    Creating table app1_model1 
    Creating table app1_model2 
    ... 

所以我想,如果我按相反的順序包括應用遷移:

heroku run python manage.py migrate sites; heroku run python manage.py migrate sessions; heroku run python manage.py migrate contenttypes; heroku run python manage.py migrate admin; heroku run python manage.py migrate flatpages; heroku run python manage.py migrate auth; 

和它的工作!我不知道爲什麼,但它確實如此。也許這也會幫助其他人。

相關問題