django manage.py抛出值错误

时间:2015-11-15 20:56:45

标签: python django

这是我运行任何manage.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

以下是models.py和settings.py中的代码,这些是我修改django应用程序的唯一地方。

models.py

    from django.db import models
from django.conf import settings
from django.contrib.auth.models import User, AbstractUser
from model_utils.fields import StatusField, MonitorField
from model_utils import Choices, FieldTracker
from jsonfield import JSONField
from django.utils.translation import gettext as _

class LastRequestStorable(models.Model):
    last_service_request = JSONField()
    req_tracker = FieldTracker()

    class Meta:
        abstract = True

class MapPointable(models.Model):
    loc_latitude = models.FloatField()
    loca_longtitude = models.FloatField()

    class Meta:
        abstract = True

class FarmStateble(models.Model):
    STATES = Choices((0, 'SLEEPING', _('Sleeping')), (1, 'SENSING', _('Sensing')), (2, 'WATERING', _('Watering')))
    state = StatusField(choices_name = 'STATES')
    state_tracker = FieldTracker(fields=['state'])

    class Meta:
        abstract = True

class Farmer(FarmStateble, MapPointable, LastRequestStorable, AbstractUser):
    last_irrigation_cycle = models.DateTimeField(auto_now_add=False, auto_now=False, null = True, blank=True)


class MoistureProfileDict(models.Model):
    name = models.CharField(max_length=30)

class MoistureKVPair(models.Model):
    profile = models.ForeignKey(MoistureProfileDict)
    sector_key = models.CharField(max_length=30)
    sector_value = models.DecimalField(max_digits=3, decimal_places=3, default=0)


class BotanicData(models.Model):
    soil_choices = Choices('LOAM', 'SILT', 'CLAY', 'SAND')
    soil_type = models.CharField(choices= soil_choices, blank=True, max_length=20)
    seed_choices = Choices('TOMATOE', 'POTATOE', 'CARROT', 'STRAWBERRY', 'CHICKEN')
    seed = models.CharField(choices=seed_choices, blank = True, max_length=15)
    root_depth = models.IntegerField(default=0)
    field_capacity = models.DecimalField(max_digits=3, decimal_places=3, default=0)
    perm_wilt_point = models.DecimalField(max_digits=3, decimal_places=3, default=0)
    max_depl = models.DecimalField(max_digits=3, decimal_places=3, default=0)


class Analytics(models.Model):
    power_used = models.DecimalField(max_digits=3, decimal_places=2, default=0)
    water_used = models.DecimalField(max_digits=3, decimal_places=2, default=0)
    water_saved = models.DecimalField(max_digits=3, decimal_places=2, default=0)


class IrrigationSession(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    botany = models.OneToOneField(BotanicData)
    analytics = models.OneToOneField(Analytics)
    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)")

settings.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 ☁

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

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作为相关名称

在models.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")

错误: irrigationservice.IrrigationSession.sector1_moist_prof :( fields.E304)&#39; IrrigationSession.sector1_moist_prof&#39;的反向访问器与反向访问者发生冲突&#39; IrrigationSession.sector2_moist_prof&#39;。

    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'.

3 个答案:

答案 0 :(得分:1)

您在related_name的格式化程序中缺少's':%(class)s

使用

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

而不是

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

(其他领域也是如此。)

答案 1 :(得分:1)

从追溯我可以说问题与related_name s有关。 我认为你必须在其中写%(class)s而不是%(class)

答案 2 :(得分:0)

我可以提示你尝试重写代码:

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将不知道使用什么样的关系

尝试:

sector1_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="sess_1")
sector2_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="sess_2")
sector3_moist_prof = models.OneToOneField(MoistureProfileDict, related_name="sess_3")