类型错误:ForeignKey(无)无效。 foreignKey 的第一个参数必须是模型、模型名称或字符串“self”

问题描述 投票:0回答:1

我对 Django 并不陌生,但对 Apache 很熟悉。我最近开发了一个 Web 应用程序并在 Nginx 上测试了它的部署,它运行得很好。不过,客户端更喜欢 Apache,我创建了一个配置文件。此后,我收到 500 服务器错误,并且上面的错误自行出现。

以下是 Apache 错误日志中的部分回溯:

[Tue Aug 06 19:48:10.945075 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
[Tue Aug 06 19:48:10.945077 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
[Tue Aug 06 19:48:10.945079 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
[Tue Aug 06 19:48:10.945082 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]   File "/var/www/mysite/vir/lib/python3.10/site-packages/django/contrib/admin/models.py", line 48, in <module>
[Tue Aug 06 19:48:10.945086 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]     class LogEntry(models.Model):
[Tue Aug 06 19:48:10.945089 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]   File "/var/www/mysite/vir/lib/python3.10/site-packages/django/contrib/admin/models.py", line 54, in LogEntry
[Tue Aug 06 19:48:10.945090 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]     user = models.ForeignKey(
[Tue Aug 06 19:48:10.945092 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]   File "/var/www/mysite/vir/lib/python3.10/site-packages/django/db/models/fields/related.py", line 959, in __init__
[Tue Aug 06 19:48:10.945094 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675]     raise TypeError(
[Tue Aug 06 19:48:10.945099 2024] [wsgi:error] [pid 344644] [remote 169.255.52.63:23675] TypeError: ForeignKey(None) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'

我打开

django/db/models/fields/related.py
,找到了异常的来源:

def __init__(
        self,
        to,
        on_delete,
        related_name=None,
        related_query_name=None,
        limit_choices_to=None,
        parent_link=False,
        to_field=None,
        db_constraint=True,
        **kwargs,
    ):
        try:
            to._meta.model_name
        except AttributeError:
            if not isinstance(to, str):
                raise TypeError(
                    "%s(%r) is invalid. First parameter to ForeignKey must be "
                    "either a model, a model name, or the string %r"
                    % (
                        self.__class__.__name__,
                        to,
                        RECURSIVE_RELATIONSHIP_CONSTANT,
                    )
                )

我知道这肯定与无法建立与 Postgres 数据库的连接有关,但到底是什么原因导致的?

  • 我尝试将数据库主机更改为服务器的主机。我收到套接字错误
  • 我还将 psql 套接字组所有权更改为 www-data,但 500 服务器错误仍然存在。

运行

./manage.py dbshell
效果很好。

这是我的 Apache 配置文件:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/mysite
        ServerName mysite.com
    Alias /static /var/www/mysite/portal/static

    <Directory /var/www/mysite/portal/static>
        Require all granted
    </Directory>
    Alias /media /home/admin/media
    <Directory /home/admin/media>
                Require all granted
        </Directory>
    <Directory /var/www/mysite/portal/portal>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    WSGIDaemonProcess portal python-path=/var/www/mysite/portal python-home=/var/www/mysite/vir
    WSGIProcessGroup portal
    WSGIScriptAlias / /var/www/mysite/portal/portal/wsgi.py
    ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

这是

wsgi.py
文件:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'portal.settings')

application = get_wsgi_application()

任何见解将不胜感激。谢谢!

python django postgresql apache mod-wsgi
1个回答
0
投票

您的

AUTH_USER_MODEL
设置[Django-doc] 以某种方式设置为
None
,例如:

# settings.py

AUTH_USER_MODEL = None

但可能不明确。例如,您可能使用了未设置的环境变量或其他变量。

看看是什么设置了

AUTH_USER_MODEL
设置,以及它如何返回
None

© www.soinside.com 2019 - 2024. All rights reserved.