执行迁移时,外键生成 'NoneType' 对象没有属性“_meta”
ForeignKey produces 'NoneType' object has no attribute '_meta' when doing a migrate
我有三个不同的应用程序:
- 人,从用户扩展
- 公司,从用户扩展而来
- Candidate,继承自 Person
在人模块中:
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'
如果我删除字段 person OneToOneField 一切正常,所以我想这可能是因为我正在与从 User 扩展的 Person 建立关系。
我怎样才能解决这个问题?
我无法更改应用程序的结构,如果因为我有两个从 Django 的用户模型扩展的模型,我认为用用户做 onetoonefield 不是一个好主意,因为它只影响人.
任何帮助将不胜感激,
谢谢!
这可能只是一个依赖性问题。
Apply all migrations: person, auth, ..., organization, account, candidate, ...
您的要求如下:
candidate.Profile 需要 person.Person 而 person.Person 需要 auth.User
您不需要 运行 再次进行迁移 auth.User。所以请执行以下操作:
./manage.py makemigrations profile
./manage.py makemigrations person
./manage.py makemigrations organization
./manage.py migrate
我已经解决了导入应用程序模块的问题,如下所示:
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)并且没有再次出现此错误。
我有三个不同的应用程序:
- 人,从用户扩展
- 公司,从用户扩展而来
- Candidate,继承自 Person
在人模块中:
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'
如果我删除字段 person OneToOneField 一切正常,所以我想这可能是因为我正在与从 User 扩展的 Person 建立关系。 我怎样才能解决这个问题? 我无法更改应用程序的结构,如果因为我有两个从 Django 的用户模型扩展的模型,我认为用用户做 onetoonefield 不是一个好主意,因为它只影响人.
任何帮助将不胜感激,
谢谢!
这可能只是一个依赖性问题。
Apply all migrations: person, auth, ..., organization, account, candidate, ...
您的要求如下: candidate.Profile 需要 person.Person 而 person.Person 需要 auth.User
您不需要 运行 再次进行迁移 auth.User。所以请执行以下操作:
./manage.py makemigrations profile
./manage.py makemigrations person
./manage.py makemigrations organization
./manage.py migrate
我已经解决了导入应用程序模块的问题,如下所示:
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)并且没有再次出现此错误。