我之前从未遇到过这个问题。当我第一次运行'makemigrations'时,我已经启动了无数个项目并且没有任何问题。在这个项目中我一直收到错误:
django.db.utils.OperationalError:没有这样的表:accounts_customuser
这就是重点,没有一张桌子......它应该是一张桌子。
我没有运行任何花哨的数据库,它只是sqlite3。
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django_extensions',
'rest_framework',
'rest_framework.authtoken',
# 'djoser',
# 'django_comments',
# 'django_comments_xtd',
'accounts',
'blog',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
完整的TraceBack:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\Lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\User\Desktop\newapp\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\base.py", line 398, in execute
self.check()
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:/Users/User/Desktop/newapp\newapp\urls.py", line 21, in <module>
from blog.views import PostViewSet
File "C:/Users/User/Desktop/newapp\blog\views.py", line 14, in <module>
class PostViewSet(ModelViewSet):
File "C:/Users/User/Desktop/newapp\blog\views.py", line 15, in PostViewSet
some_user = CustomUser.objects.get(pk=1)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\query.py", line 381, in get
num = len(clone)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\query.py", line 240, in __len__
self._fetch_all()
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\query.py", line 52, in __iter__
results = compiler.execute_sql()
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\User\Desktop\newsite\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: accounts_customuser
编辑#2
问题可能出在我的PostViewSet中吗?我之前从未做过复杂的查询,所以我不知道它是否是正确的语法。我是从here
得到的class PostViewSet(ModelViewSet):
some_user = CustomUser.objects.get(pk=1)
queryset = Post.objects.filter(
Q(poster=some_user) |
Q(poster__friends__creator=some_user) |
Q(poster__friendship_creator__friend=some_user)).distinct()
serializer_class = PostSerializer
答案 0 :(得分:2)
问题是,在加载URL配置时,您的CustomUser.objects.get(pk=1)
查询正在运行,并在创建表之前导入视图。您可以通过将代码移动到get_queryset
方法来解决此问题。
class PostViewSet(ModelViewSet):
def get_queryset(self):
some_user = CustomUser.objects.get(pk=1)
return Post.objects.filter(
Q(poster=some_user) |
Q(poster__friends__creator=some_user) |
Q(poster__friendship_creator__friend=some_user)).distinct()
serializer_class = PostSerializer