Django模型字段名称"check"引发SystemCheckError [英] Django model field name "check" raises SystemCheckError
问题描述
Django文档状态模型字段名称只有两个限制
The Django docs state there are only two restrictions on model field names
- 字段名称不能是Python保留字
- 字段名称不能连续包含多个下划线
但是,在下面的示例中,看起来我不能使用字段名称 check
作为ForeignKey.
However, given the following example, it doesn't look like I can use the field name check
as a 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>
文档错了吗,还是我做错了什么?
Are the docs wrong, or am I doing something wrong?
编辑:忘记提及该项目使用的是Python 2和Django 1.11
forgot to mention this project is using Python 2 and Django 1.11
推荐答案
I found System check framework in django docs (https://docs.djangoproject.com/en/2.2/ref/checks/#system-check-framework)
在django的系统检查期间调用的检查字段会引发SystemCheckError.
Your check field called during django's system checks so raised SystemCheckError.
它发生在Django的 Basecommand
( https://github.com/django/django/blob/1e87c9fe71703fab23039aa63fafe4f6aac98bbc/django/core/management/base.py#L148 )
It occurred in django's 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``.
这篇关于Django模型字段名称"check"引发SystemCheckError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!