为什么部署过程返回错误以下?

时间:2019-01-29 04:20:36

标签: python django python-3.x heroku

我正在heroku上部署python应用程序, setting.py 中的连接字符串如下所示:

import dj_database_url   ####not working for my case
        DATABASES = {
            'default': dj_database_url.config(
                default=os.environ.get('DATABASE_URL')
            )
        }

requirement.txt 中的依赖项已成功部署。
以下是我尝试部署后遇到的错误,欢迎您提供任何帮助。

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
                                                             ^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 332, in execute
    self.check()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 58, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/app/restful01/urls.py", line 19, in <module>
    from .api import router
  File "/app/restful01/api.py", line 2, in <module>
    from toys import api_views as toys_views
  File "/app/toys/api_views.py", line 6, in <module>
    class allroom(viewsets.ModelViewSet):
  File "/app/toys/api_views.py", line 7, in allroom
    queryset = models.Toy.objects.all()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 155, in all
    return self.get_queryset()
  File "/app/.heroku/src/django-moderation/moderation/managers.py", line 129, in get_queryset
    return self.filter_moderated_objects(query_set)
  File "/app/.heroku/src/django-moderation/moderation/managers.py", line 42, in filter_moderated_objects
    if annotated_queryset.exists():
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 715, in exists
    return self.query.has_results(using=self.db)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py", line 509, in has_results
    return compiler.has_results()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1032, in has_results
    return bool(self.execute_sql(SINGLE))
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1050, in execute_sql
    sql, params = self.as_sql()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 457, in as_sql
    from_, f_params = self.get_from_clause()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 719, in get_from_clause
    clause_sql, clause_params = self.compile(from_clause)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 390, in compile
    sql, params = node.as_sql(self, self.connection)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/datastructures.py", line 84, in as_sql
    compiler.query.where_class, self.table_alias, self.parent_alias)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/reverse_related.py", line 137, in get_extra_restriction
    return self.field.get_extra_restriction(where_class, related_alias, alias)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/contenttypes/fields.py", line 462, in get_extra_restriction
    contenttype_pk = self.get_content_type().pk
  File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/contenttypes/fields.py", line 458, in get_content_type
    for_concrete_model=self.for_concrete_model)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/contenttypes/models.py", line 51, in get_for_model
    ct = self.get(app_label=opts.app_label, model=opts.model_name)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 397, in get
    num = len(clone)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 254, in __len__
    self._fetch_all()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...

2 个答案:

答案 0 :(得分:0)

在网上找到了这个。 您需要安装virtualenv和pip。在virtualenv中工作(已激活),您需要做

pip install dj-database-url

然后您需要做:

pip freeze > requirements.txt

可能值得一试。

答案 1 :(得分:0)

django-moderation实际上是一个问题。您可能在某个地方引用了仲裁模型的ForeignKey的自定义窗体。这将触发django-moderation代码中的数据库查找,并且当django没有所需的表时(在运行./manage.py迁移之前),它将失败。

我们当前的“尽力而为”解决方案方法:

class SomeForm(forms.ModelForm):
    class Meta:
        model = SomeModel
        fields = [
            'problematic_field_reference_to_foreign_model_that_is_moderated',
            'some_other_field',
            'another_field',
        ]
        if ('makemigrations' in sys.argv or 'migrate' in sys.argv):
            fields.remove("problematic_field_reference_to_foreign_model_that_is_moderated")

我当然不为此感到骄傲,但我需要继续前进。