Question

I have two classes which import each other:

profile/models.py

class Company(models.Model):
    name = ...

class CompanyReview(models.Model):
    company = models.ForeignKey(Company)
    from action.models import CompanyAction
    action = models.ForeignKey(CompanyAction)

action/models.py

from profile.models import Company
class CompanyAction(models.Model):
    company = models.ForeignKey(Company, null = True, blank = True)

The circular import works when the Django app is executed on the server or when I call view functions in the shell. However, when I import one of the classes, Django command will fail with an error (see Traceback below).

Why is that the case and only causing a problem in the command method? How can I avoid the error? I have tried a lazy import of the CompanyAction class, but it led to the same error message.

not working alternative:

class CompanyReview(models.Model):
    company = models.ForeignKey(Company)
    from django.db.models import get_model
    _model = get_model('action', 'CompanyAction')
    action = models.ForeignKey(_model)

Interestingly, the variable _model is empty if I execute my command function and the classes are imported. When I load ./manage.py shell, the variable contains the correct class name. Why is that the case?

Traceback

(virtual-env)PC:neurix$ python manage.py close_action
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/Development/virtual-re/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 "/Users/Development/virtual-re/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/Development/project/apps/action/management/commands/close_action.py", line 2, in <module>
    from action.models import CompanyAction
  File "/Users/Development/project/apps/action/models.py", line 26, in <module>
    from profile.models import Company
  File "/Users/Development/apps/profile/models.py", line 436, in <module>
    class CompanyReview(models.Model):
  File "/Users/Development/project/apps/profile/models.py", line 446, in CompanyReview
    action = models.ForeignKey(_model)
  File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/db/models/fields/related.py", line 993, in __init__
    assert isinstance(to, six.string_types), "%s(%r) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string %r" % (self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
AssertionError: ForeignKey(None) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'
Was it helpful?

Solution

Django has a system for stopping circular imports on foreign keys detailed here: https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

You would want to do something like:

class CompanyReview(models.Model):
    company = models.ForeignKey(Company)
    action = models.ForeignKey('action.CompanyAction')

class CompanyAction(models.Model):
    company = models.ForeignKey('profile.Company', null = True, blank = True)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top