gpt4 book ai didi

python - Django 模型字段名称 "check"引发 SystemCheckError

转载 作者:行者123 更新时间:2023-12-05 07:25:52 25 4
gpt4 key购买 nike

Django docs声明对模型字段名称只有两个限制

  • 字段名不能是 Python 保留字
  • 一个字段名不能连续包含一个以上的下划线

但是,在下面的示例中,我似乎无法将字段名称 check 用作 ForeignKey。

class Check(models.Model):

name = models.CharField(max_length=100)

class MyModel(models.Model):

# this works fine
#check = models.BooleanField()

# this breaks
check = models.ForeignKey(Check, on_delete=models.PROTECT, related_name='+')

这是错误:

$ python manage.py check
SystemCheckError: System check identified some issues:

ERRORS:
myapp.MyModel: (models.E020) The 'MyModel.check()' class method is currently overridden by <django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x03A818D0>

是文档错了,还是我做错了什么?

编辑:忘了说这个项目使用的是 Python 2 和 Django 1.11

最佳答案

我在 django 文档中找到了系统检查框架 ( https://docs.djangoproject.com/en/2.2/ref/checks/#system-check-framework )

在 django 的系统检查期间调用的检查字段引发了 SystemCheckError。

它发生在 django 的 Basecommand ( https://github.com/django/django/blob/1e87c9fe71703fab23039aa63fafe4f6aac98bbc/django/core/management/base.py#L148 )

1. ``django-admin`` or ``manage.py`` loads the command class
and calls its ``run_from_argv()`` method.
2. The ``run_from_argv()`` method calls ``create_parser()`` to get
an ``ArgumentParser`` for the arguments, parses them, performs
any environment changes requested by options like
``pythonpath``, and then calls the ``execute()`` method,
passing the parsed arguments.
3. The ``execute()`` method attempts to carry out the command by
calling the ``handle()`` method with the parsed arguments; any
output produced by ``handle()`` will be printed to standard
output and, if the command is intended to produce a block of
SQL statements, will be wrapped in ``BEGIN`` and ``COMMIT``.
4. If ``handle()`` or ``execute()`` raised any exception (e.g.
``CommandError``), ``run_from_argv()`` will instead print an error
message to ``stderr``.

关于python - Django 模型字段名称 "check"引发 SystemCheckError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54681167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com