我正在尝试运行现有的Django项目,但始终会收到相同的错误
运行python manage.py后,我发现错误关系不存在。我正在使用Django 1.11.11版。我认为数据库没有错误,因为当我将Django版本降级到1.8.1时,没有错误并且迁移成功。可能是什么问题?
Traceback (most recent call last):
File "C:\Vizia_project\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "qa_qagroup" does not exist
LINE 1: SELECT (1) AS "a" FROM "qa_qagroup" LIMIT 1
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "C:\Vizia_project\env\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
utility.execute()
File "C:\Vizia_project\env\lib\site-packages\django\core\management\__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Vizia_project\env\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Vizia_project\env\lib\site-packages\django\core\management\base.py", line 327, in execute
self.check()
File "C:\Vizia_project\env\lib\site-packages\django\core\management\base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "C:\Vizia_project\env\lib\site-packages\django\core\management\commands\migrate.py", line 62, in _run_checks
issues.extend(super(Command, self)._run_checks(**kwargs))
File "C:\Vizia_project\env\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Vizia_project\env\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Vizia_project\env\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "C:\Vizia_project\env\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
return check_method()
File "C:\Vizia_project\env\lib\site-packages\django\urls\resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "C:\Vizia_project\env\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Vizia_project\env\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Vizia_project\env\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Vizia_project\env\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Vizia_project\env\lib\importlib\__init__.py", line 104, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1448, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Vizia_project\banknote\urls.py", line 24, in <module>
from qa.views import *
File "C:\Vizia_project\qa\views.py", line 11, in <module>
class QAGroupFilter(django_filters.FilterSet):
File "C:\Vizia_project\qa\views.py", line 13, in QAGroupFilter
if QAGroup.objects.exists():
File "C:\Vizia_project\env\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Vizia_project\env\lib\site-packages\django\db\models\query.py", line 670, in exists
return self.query.has_results(using=self.db)
File "C:\Vizia_project\env\lib\site-packages\django\db\models\sql\query.py", line 517, in has_results
return compiler.has_results()
File "C:\Vizia_project\env\lib\site-packages\django\db\models\sql\compiler.py", line 858, in has_results
return bool(self.execute_sql(SINGLE))
File "C:\Vizia_project\env\lib\site-packages\django\db\models\sql\compiler.py", line 899, in execute_sql
raise original_exception
File "C:\Vizia_project\env\lib\site-packages\django\db\models\sql\compiler.py", line 889, in execute_sql
cursor.execute(sql, params)
File "C:\Vizia_project\env\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Vizia_project\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Vizia_project\env\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Vizia_project\env\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Vizia_project\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "qa_qagroup" does not exist
LINE 1: SELECT (1) AS "a" FROM "qa_qagroup" LIMIT 1
^
requirements.txt looks this:
beautifulsoup4==4.6.0
certifi==2017.7.27.1
chardet==3.0.4
Django==1.11.11
django-crispy-forms==1.7.1
django-filter==1.1.0
django-modelcluster==3.1
django-session-security==2.6.0
django-taggit==0.22.1
django-treebeard==4.1.2
djangorestframework==3.6.4
html5lib==0.999999999
idna==2.6
olefile==0.44
Pillow==4.3.0
psycopg2==2.7.3.2
python-dateutil==2.6.1
pytz==2017.2
raven==6.3.0
requests==2.18.4
six==1.11.0
Unidecode==0.4.21
urllib3==1.22
wagtail==1.12.2
webencodings==0.5.1
wheel==0.24.0
Willow==0.4
来自qa的views.py
from django.shortcuts import render
import django_filters
from qa.models import QAGroup
from qa.forms import QAForm
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
# Create your views here.
class QAGroupFilter(django_filters.FilterSet):
if QAGroup.objects.exists():
initial = QAGroup.objects.first().pk
else:
initial = None
group = django_filters.ModelChoiceFilter(
queryset=QAGroup.objects.order_by('pk'),
# widget=LinkWidget(),
label='',
empty_label=None,
name='title',
initial=initial,
)
def __init__(self, data=None, *args, **kwargs):
# filterset is bound, use initial values as defaults
if data is not None:
data = data.copy()
for name, f in self.base_filters.items():
initial = f.extra.get('initial')
# filter param is either missing or empty, use initial as default
if not data.get(name) and initial:
data[name] = initial
super(QAGroupFilter, self).__init__(data, *args, **kwargs)
class Meta:
model = QAGroup
fields = ['group', ]
def qa_list(request):
filter = QAGroupFilter(request.GET, queryset=QAGroup.objects.all())
groups = QAGroup.objects.order_by('pk')
form = QAForm()
if request.method == 'POST':
form = QAForm(request.POST)
if form.is_valid():
form.send_email()
messages.add_message(request, messages.INFO, _('Some text here'))
return redirect('faq')
return render(request, 'qa/qa.html', {'filter': filter, 'groups': groups, 'form': form})
答案 0 :(得分:0)
这是一个常见问题。
您在类级别拥有代码,因此它可以在导入上运行-在迁移可以运行之前。对于您而言,代码位于QAGroupFilter中,您可以在其中进行pip install -q pytest
。该代码需要移到方法中。
答案 1 :(得分:0)
谢谢大家的帮助。解决了!我刚刚删除了QAGroup.objects.exists()if语句