我已经查看了有关此错误的答案,因此我的问题是具体说明为什么在包含以下行时会出现此错误?:
from teams.models import clubs
我不明白为什么导入会导致问题。
追溯是:
File "c:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
1012, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "c:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line
146, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in __ge
tattr__
self._setup(name)
File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _set
up
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TAB
LESPACE, but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing s
ettings.
" populate_players.py':
的完整代码import os
import sys
import django
from teams.models import clubs
club1 = "Arsenal"
club2 = clubs.objects.get(name = club1);
def add_players(name, position, born, height, weight, nationality, club=club2, active=True):
# run the script per club
p = players.objects.get_or_create(name=name, position=position, born=born, height=height, weight=weight, nationality=nationality)
return p
def populate():
add_players(name='Calum Chambers',
position = 'DEF', # list
born = '2015-01-20',
height = '1.83',
weight = '65',
nationality = 'England'
)
for p in players.objects.all():
print p
if __name__ == '__main__':
print "Starting player population script..."
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "epl.settings")
django.setup()
from teams.models import players
populate()
和models.py:
from django.db import models
# Create your models here.
class clubs(models.Model):
name = models.CharField(max_length=64)
nickname = models.CharField(max_length=64)
logo = models.ImageField(blank=True)
manager = models.CharField(max_length=128)
established = models.IntegerField()
stadium = models.CharField(max_length=64)
active = models.BooleanField(default=True)
# Fixtures, form, league position?
def __unicode__(self):
return self.name
class players(models.Model):
GOALKEEPER = 'GK'
DEFENDER = 'DEF'
MIDFIELDER = 'MID'
FORWARD = 'FWD'
POSITION_CHOICES = (
(GOALKEEPER, 'Goalkeeper'),
(DEFENDER, 'Defender'),
(MIDFIELDER, 'Midfielder'),
(FORWARD, 'Forward'),
)
name = models.CharField(max_length=64)
club = models.OneToOneField('clubs', related_name= "team")
position = models.CharField(max_length=64, choices=POSITION_CHOICES, default=GOALKEEPER) # GK, Defender, Midfielder, Attacker - Field for this?
born = models.DateField()
height = models.DecimalField(max_digits=3, decimal_places=2)
weight = models.IntegerField() # Field for this?
nationality = models.CharField(max_length=64) # Choices
active = models.BooleanField(default=True) # active = current epl player
slug = models.SlugField()
def __unicode__(self):
return self.name
答案 0 :(得分:2)
如果您在本地而不是通过django服务器调用此脚本,则需要在导入之前导入/配置设置,例如你的模特:
from django.conf import settings
settings.configure(DEBUG=True, TEMPLATE_DEBUG=True,
TEMPLATE_DIRS=('/home/web-apps/myapp', '/home/web-apps/base'))
您可以找到更多详情here。