简体   繁体   English

在Django中导入模型时出错

[英]Error while importing a model in django

I got this traceback while importing my model in management/commands: 在管理/命令中导入模型时得到了此追溯:

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

What's wrong?? 怎么了?? Django admin page works great. Django管理页面效果很好。
My model: 我的模特:

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'

You gave Django a top-level module, where it expects models to be part of a package. 您为Django提供了一个顶层模块,该模块期望模型成为软件包的一部分。 The error is the result of Django looking for the package name, and it doesn't exist here. 该错误是Django查找软件包名称的结果,此处不存在。

Do not add the package your models.py file lives in to the PYTHONPATH; 不要将您的models.py文件所在的包添加到PYTHONPATH中; only add directory where the parent package lives, here Project/apps . 仅添加父包所在的目录,在此处Project/apps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM