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; 

和它的工作!我不知道为什么,但它确实如此。也许这也会帮助其他人。

相关问题