Python 在django中导入模型时出错
我在管理/命令中导入模型时得到此回溯:Python 在django中导入模型时出错,python,django,Python,Django,我在管理/命令中导入模型时得到此回溯: Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/home/vagrant/.virtualenvs/rsspainter/local/lib/python2.7/site-packages/django/core/management/__ini
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/vagrant/.virtualenvs/rsspainter/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/home/vagrant/.virtualenvs/rsspainter/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vagrant/.virtualenvs/rsspainter/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/vagrant/.virtualenvs/rsspainter/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/vagrant/.virtualenvs/rsspainter/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/vagrant/rsspainter/apps/account/management/commands/create_users.py", line 5, in <module>
from models import *
File "/vagrant/rsspainter/apps/account/models.py", line 5, in <module>
class SeoUser(models.Model):
File "/home/vagrant/.virtualenvs/rsspainter/local/lib/python2.7/site-packages/django/db/models/base.py", line 93, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range
您为Django提供了一个顶级模块,它希望模型成为包的一部分。这个错误是Django查找包名的结果,但它在这里不存在
不要将您的
models.py
文件所在的包添加到PYTHONPATH中;仅添加父包所在的目录,此处Project/apps
此模块的名称是什么?Django希望有一个顶级包,其中包含此项,但您的模块似乎没有包父级。Project/apps/account/models.py
和Project/apps/account/management/commands/create.py
Project/apps/account/
位于PYTHONPATH
尝试将PYTHONPATH
更改为Project/apps/
。如果这不起作用,请将从models import*
更改为从.models import*
或从.models import*
。它不应位于python路径中;这意味着模型
现在是顶级模块Project/apps
应该在你的PYTHONPATH
中,帐户应该是一个包;e、 有一个\uuuu init\uuuu.py
文件。谢谢。。。这很有效。我是个白痴。
class SeoUser(models.Model):
'''
Model of user.
'''
SEX = (
('M', 'Male'),
('F', 'Female'),
)
THEME_CHOICES = (
('auto', u'Автомобили'),
('tourism', u'Туризм'),
('cooking', u'Готовка'),
('gambling', u'Гэмблинг'),
('games', u'Онлайн игры'),
('business', u'Бизнесс'),
('amusement', u'Развлечения'),
('sports', u'Спорт и здоровье'),
('nyan', u'Животные'),
('quotes', u'Цитатники'),
('humor', u'Юмор'),
('women', u'Женские секреты'),
)
name = models.CharField("Name", max_length=100, blank=False)
surname = models.CharField("Surname",max_length=150, blank=False)
password = models.CharField("Password", max_length=16)
country = models.CharField("Country", max_length=25)
town = models.CharField("Town", max_length=35)
is_real = models.BooleanField("Real user", default=False)
sex = models.CharField("Sex", max_length=1, choices=SEX)
age = models.IntegerField("Age", blank=False)
thematics = models.CharField("Specification", max_length=15, choices=THEME_CHOICES, default='amusement')
def __unicode__(self):
return "%s-%s" % (self.name, self.thematics)
# links to related models
def email_count(self):
return self.email.count()
email_count.short_description = 'Email accounts'
def blog_count(self):
return self.blog.count()
blog_count.short_description = 'Blogs'
def twitter_count(self):
return self.twitter.count()
twitter_count.short_description = 'Twitter accounts'