Django 管理.py 抛出的值

2024-04-24 09:34:35 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我运行任何管理.py命令

    Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Library/Python/2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/krishna/Documents/irrigationwebserver/webserver/irrigationservice/models.py", line 61, in <module>
    class IrrigationSession(models.Model):
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 189, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1783, in contribute_to_class
    super(ForeignObject, self).contribute_to_class(cls, name, virtual_only=virtual_only)
  File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 305, in contribute_to_class
    'app_label': cls._meta.app_label.lower()
ValueError: incomplete format

这是密码模型.py以及设置.py,这是我修改django应用程序的唯一地方。在

在模型.py在

^{pr2}$

在设置.py在

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=%m-!gjz88m15lo^buqxuhqv7pv0ih_9)x+v#_dxub#ye&jbry'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'irrigationservice',
)

AUTH_USER_MODEL = 'irrigationservice.Farmer'

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'webserver.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'webserver.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

我试着用谷歌搜索错误或搜索堆栈溢出来寻找一些答案,但我看不出我做错了什么。在

编辑1: 在采纳了修改相关名称的建议后,我将它们分别改为sess_1 sess_2和sess_3。但现在我遇到了一个我试图避免的错误。在

python manage.py makemigrations                                                            ✗ krish/modelsdefintion ☁

系统检查错误:系统检查发现了一些问题:

ERRORS:
irrigationservice.IrrigationSession.sector2_moist_prof: (fields.E304) Reverse accessor for 'IrrigationSession.sector2_moist_prof' clashes with reverse accessor for 'IrrigationSession.sector3_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector2_moist_prof' or 'IrrigationSession.sector3_moist_prof'.
irrigationservice.IrrigationSession.sector2_moist_prof: (fields.E305) Reverse query name for 'IrrigationSession.sector2_moist_prof' clashes with reverse query name for 'IrrigationSession.sector3_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector2_moist_prof' or 'IrrigationSession.sector3_moist_prof'.
irrigationservice.IrrigationSession.sector3_moist_prof: (fields.E304) Reverse accessor for 'IrrigationSession.sector3_moist_prof' clashes with reverse accessor for 'IrrigationSession.sector2_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector3_moist_prof' or 'IrrigationSession.sector2_moist_prof'.
irrigationservice.IrrigationSession.sector3_moist_prof: (fields.E305) Reverse query name for 'IrrigationSession.sector3_moist_prof' clashes with reverse query name for 'IrrigationSession.sector2_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector3_moist_prof' or 'IrrigationSession.sector2_moist_prof'.

编辑2:使用%(class)s作为相关名称

变化模型.py在

    sector1_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="%(class)s")
sector2_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="%(class)s")
sector3_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="%(class)s")

错误: 灌溉服务。灌溉会议。第1节潮湿教授:(fields.E304)的反向访问器'灌溉会议。第1节;潮湿;教授'与反向访问器冲突'灌溉会议。第2节\u潮湿教授'. 在

    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector1_moist_prof' or 'IrrigationSession.sector2_moist_prof'.
irrigationservice.IrrigationSession.sector1_moist_prof: (fields.E304) Reverse accessor for 'IrrigationSession.sector1_moist_prof' clashes with reverse accessor for 'IrrigationSession.sector3_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector1_moist_prof' or 'IrrigationSession.sector3_moist_prof'.
irrigationservice.IrrigationSession.sector1_moist_prof: (fields.E305) Reverse query name for 'IrrigationSession.sector1_moist_prof' clashes with reverse query name for 'IrrigationSession.sector2_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector1_moist_prof' or 'IrrigationSession.sector2_moist_prof'.
irrigationservice.IrrigationSession.sector1_moist_prof: (fields.E305) Reverse query name for 'IrrigationSession.sector1_moist_prof' clashes with reverse query name for 'IrrigationSession.sector3_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector1_moist_prof' or 'IrrigationSession.sector3_moist_prof'.
irrigationservice.IrrigationSession.sector2_moist_prof: (fields.E304) Reverse accessor for 'IrrigationSession.sector2_moist_prof' clashes with reverse accessor for 'IrrigationSession.sector1_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector2_moist_prof' or 'IrrigationSession.sector1_moist_prof'.
irrigationservice.IrrigationSession.sector2_moist_prof: (fields.E304) Reverse accessor for 'IrrigationSession.sector2_moist_prof' clashes with reverse accessor for 'IrrigationSession.sector3_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector2_moist_prof' or 'IrrigationSession.sector3_moist_prof'.
irrigationservice.IrrigationSession.sector2_moist_prof: (fields.E305) Reverse query name for 'IrrigationSession.sector2_moist_prof' clashes with reverse query name for 'IrrigationSession.sector1_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector2_moist_prof' or 'IrrigationSession.sector1_moist_prof'.
irrigationservice.IrrigationSession.sector2_moist_prof: (fields.E305) Reverse query name for 'IrrigationSession.sector2_moist_prof' clashes with reverse query name for 'IrrigationSession.sector3_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector2_moist_prof' or 'IrrigationSession.sector3_moist_prof'.
irrigationservice.IrrigationSession.sector3_moist_prof: (fields.E304) Reverse accessor for 'IrrigationSession.sector3_moist_prof' clashes with reverse accessor for 'IrrigationSession.sector1_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector3_moist_prof' or 'IrrigationSession.sector1_moist_prof'.
irrigationservice.IrrigationSession.sector3_moist_prof: (fields.E304) Reverse accessor for 'IrrigationSession.sector3_moist_prof' clashes with reverse accessor for 'IrrigationSession.sector2_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector3_moist_prof' or 'IrrigationSession.sector2_moist_prof'.
irrigationservice.IrrigationSession.sector3_moist_prof: (fields.E305) Reverse query name for 'IrrigationSession.sector3_moist_prof' clashes with reverse query name for 'IrrigationSession.sector1_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector3_moist_prof' or 'IrrigationSession.sector1_moist_prof'.
irrigationservice.IrrigationSession.sector3_moist_prof: (fields.E305) Reverse query name for 'IrrigationSession.sector3_moist_prof' clashes with reverse query name for 'IrrigationSession.sector2_moist_prof'.
    HINT: Add or change a related_name argument to the definition for 'IrrigationSession.sector3_moist_prof' or 'IrrigationSession.sector2_moist_prof'.

Tags: ortodjangonamepyforservicerelated
3条回答

通过回溯,我可以说这个问题与related_names有关。 我想你应该写%(class)s而不是{}

相关名称的格式化程序中缺少“s”:%(class)s。在

使用

sector1_moist_prof = models.OneToOneField(MoistureProfileDict, \
related_name="%(class)s")

而不是

^{pr2}$

(其他字段同上。)

我建议你试着重写代码:

sector1_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="%(class)")
sector2_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="%(class)")
sector3_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="%(class)")

你被声明为三个一对一关系,但给他们所有的一个反向名称,这不是肯定的-如果你尝试做反向-django orm将不知道使用什么样的execly关系

尝试:

^{pr2}$

相关问题 更多 >