Search code examples
djangodjango-modelsmodels

ForeignKey produces 'NoneType' object has no attribute '_meta' when doing a migrate


I have three different applications:

  1. Person, which extends from a User
  2. Company, which extends from a User
  3. Candidate, which extends from Person

In the person module:

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)

In the organization one, it's very similar from the person:

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)

And finally, in the candidate app:

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"

When i do makemigrations, everything works ok but when doing the migrate:

    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'

If I delete the field person OneToOneField everything works okay, so I imagine it can be caused by the fact that I'm doing the relation with Person which extends from User. How can I fix this? I can't change the structure of apps and if because of the fact that I have two models extending from the User model of Django, I think it cannot be a good idea to do the onetoonefield with User, because it only affects on the Person.

Any help will be appreciated,

Thanks!


Solution

  • I've solved the issue importing the application module as it follows:

    from person import models as person_class
    

    And then, in the field of the OneToOneField:

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

    EDIT:
    Wrong, the error came out again. Tired of searching, I've changed the project to Django 1.7 (before was django 1.8) and this error hasn't appeared again.