6

安装 Django-userena 后,我的 django 版本出现错误:1.9.5 我只是一步一步安装 django-userena,但是当我迁移它时,发生了一个错误,我不知道如何解决它。

    Traceback (most recent call last):
  File "manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    emit_post_migrate_signal(self.verbosity, self.interactive, connection.alias)
  File "C:\Python27\lib\site-packages\django\core\management\sql.py", line 50, in emit_post_migrate_signal
    using=db)
  File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py", line 192, in send
    response = receiver(signal=self, sender=sender, **named)
  File "C:\Python27\lib\site-packages\guardian\management\__init__.py", line 33, in create_anonymous_user
    User.objects.get(**lookup)
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 381, in get
    num = len(clone)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 240, in __len__
    self._fetch_all()
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_user

应用:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    'userena',
    'guardian',
    'easy_thumbnails',
    'accounts',    
]
4

7 回答 7

9
./manage.py migrate

如果您的 Django 版本是 1.9 或更低,请使用

./manage.py syncdb

然后

python manage.py createsuperuser

有关https://github.com/django/django/blob/master/django/contrib/auth/forms.py的更多详细信息

愿它有所帮助

于 2016-10-09T14:00:04.980 回答
4

问题是这一行:

File "C:\Python27\lib\site-packages\guardian\management\__init__.py", line 33, in create_anonymous_user User.objects.get(**lookup)

guardian\management\__init__.pyUser在定义之前调用。

为什么在定义之前调用它?...好吧,它被调用makemigrations来找出db定义它们的变化(找到的变化)

...所以这是一个“鸡和蛋”的问题。

我有类似问题的相同错误,但有问题的代码是我自己的代码(不是类似的库guardian

我的代码是这样的:

CHAT_BOT_USER, created = User.objects.get_or_create(username='rosty', \ email=settings.EMAIL_HOST_USER)

有问题的部分是在创建表User之前使用的auth_user

我通过仅在表存在时执行代码来解决。当没有OperationError. 您可以通过以下方式了解它try/except

from django.db import OperationalError

try:
    CHAT_BOT_USER, created = User.objects.get_or_create(username='rosty', email=settings.EMAIL_HOST_USER)

except OperationalError:
    CHAT_BOT_USER = None

if created:
    CHAT_BOT_USER.set_password(settings.EMAIL_HOST_PASSWORD)
    CHAT_BOT_USER.save()

这种方式makemigrations运行except代码并runserver运行try代码。

注意created布尔值,您可以使用任何其他方式来避免makemigration运行取决于User.objects....结果的代码

于 2017-06-20T08:24:20.677 回答
4

我得到了同样的错误并修复了同样的错误,我检查了所提到的错误表是否存在于连接的数据库中。不是。再次应用迁移,它起作用了。

python manage.py migrate

python manage.py makemigrations
于 2018-11-01T19:43:29.177 回答
3
  python manage.py migrate --run-syncdb  

该命令将查看哪些表未创建,并将创建所有需要的表。

于 2021-02-19T20:42:34.490 回答
0

运行:python3 manage.py 迁移

于 2020-05-24T12:27:59.050 回答
0

我在使用聊天室时遇到了同样的错误。我解决了使用删除db.sqlite3文件并删除应用程序迁移文件夹中的0001_initial.py文件,然后执行以下语句:-

python manage.py makemigrations
python manage.py migrate

然后我能够使用创建超级用户

python manage.py createsuperuser

这就是它的错误解决!

于 2021-03-24T05:39:11.607 回答
0

只要未创建表,或者此应用程序指向的数据库中的表不可用,就会发生这种情况settings.py

因此,使用以下命令创建迁移文件:

python manage.py makemigrations

创建迁移文件后,您需要迁移它们:

python manage.py migrate

如果它不起作用,请使用:

python manage.py makemigrations yourappname
python manage.py migrate yourappname
于 2021-11-12T11:00:12.067 回答