ForeignKey生成' NoneType'对象没有属性' _meta'进行迁移时

时间:2015-07-03 09:09:10

标签: django django-models models

我有三种不同的应用程序:

  1. 从用户
  2. 延伸的人
  3. 公司,从用户
  4. 延伸
  5. 候选人,来自人
  6. 模块中:

    class Person(User):
        introductory_text = models.TextField(verbose_name=_(u"Introductory text"), blank=True, null=True)
        image = FilerImageField(null=True, blank=True, verbose_name="Image", related_name='image_person')
    
        class Meta:
            verbose_name = _(u"Person")
            verbose_name_plural = _(u"People")
    
        def __str__(self):
            return str(self.email)
    

    组织中,它与此人非常相似:

    class Organization(User):
        organization_name = models.CharField(verbose_name=_(u"Organiztion name"), max_length=40)
        CIF = models.CharField(verbose_name=_("CIF"), max_length=9)
    
        class Meta:
            verbose_name=_(u"Organization")
            verbose_name_plural=_(u"Organizations")
    
        def __str__(self):
            return str(self.organization_name)
    

    最后,在候选人应用中:

    class Profile(models.Model):
        person = models.OneToOneField('person.Person', related_name='profile_person')
        mute_offer = models.BooleanField(verbose_name=_(u"Mute offers?"))
    
        class Meta:
            verbose_name = _(u"Profile")
            verbose_name_plural = _(u"Profiles")
    
        def __str__(self):
            return "Profile"
    

    当我做makemigrations时,一切正常,但在进行迁移时:

        Operations to perform:
      Synchronize unmigrated apps: staticfiles, modeltranslation, redactor, messages, linkedin, bootstrap_admin, allauth, facebook, google
      Apply all migrations: person, auth, sites, easy_thumbnails, admin, contenttypes, django_messages, filer, notifications, sessions, push_notifications, organization, account, candidate, socialaccount
    Synchronizing apps without migrations:
      Creating tables...
        Running deferred SQL...
      Installing custom SQL...
    Running migrations:
      Rendering model states...Traceback (most recent call last):
      File "./manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
        utility.execute()
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
        output = self.handle(*args, **options)
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 221, in handle
        executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/db/migrations/executor.py", line 104, in migrate
        state = migration.mutate_state(state, preserve=do_run)
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/db/migrations/migration.py", line 83, in mutate_state
        operation.state_forwards(self.app_label, new_state)
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 51, in state_forwards
        state.reload_model(app_label, self.model_name_lower)
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/db/migrations/state.py", line 122, in reload_model
        related_models.update(get_related_models_recursive(rel_model))
      File "/home/karlie/.virtualenvs/tuwing/lib/python3.4/site-packages/django/db/migrations/state.py", line 57, in get_related_models_recursive
        rel_app_label, rel_model_name = rel_mod._meta.app_label, rel_mod._meta.model_name
    AttributeError: 'NoneType' object has no attribute '_meta'
    

    如果我删除字段人OneToOneField一切正常,所以我想这可能是因为我与从User扩展的Person的关系。 我怎样才能解决这个问题? 我无法改变应用程序的结构,如果因为我有两个模型从Django的User模型扩展,我认为用User进行onetoonefield并不是一个好主意,因为它只会影响到此人。

    任何帮助将不胜感激,

    谢谢!

2 个答案:

答案 0 :(得分:0)

这可能只是一个依赖问题。

./manage.py makemigrations profile
./manage.py makemigrations person
./manage.py makemigrations organization
./manage.py migrate

您的要求如下: candidate.Profile需要person.Person和person.Person需要auth.User

您不需要再次使用auth.User运行makemigrations。请执行以下操作:

#include <string>
#include <sstream>

template<int index> double getValue() 
{
    std::stringstream ss("Not implemented for index ");
    ss << index;
    throw std::exception(ss.str()); 
}

template<> double getValue<0>() { return 3.6; }
template<> double getValue<1>() { return 7.77; }

template<int index> double multiply(double x)
{
    return getValue<index>() * x;
}

答案 1 :(得分:0)

我已经解决了导入应用程序模块的问题:

from person import models as person_class

然后,在OneToOneField的领域:

person = models.OneToOneField(person_class.Person, verbose_name=_(u"Person"), related_name="person")

修改
错了,错误又出来了。厌倦了搜索,我已将项目更改为Django 1.7(以前是django 1.8)并且此错误未再出现。