Django makemigrations: ValueError: Lookup failed for model referenced by field

Django makemigrations: ValueError: Lookup failed for model referenced by field

我收到了这个错误。

  Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 125, in handle
    migration_name=self.migration_name,
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/autodetector.py", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/autodetector.py", line 110, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/state.py", line 158, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/state.py", line 236, in __init__
    raise ValueError(msg.format(field=operations[0][1], model=lookup_model)) ValueError: Lookup failed for model referenced by field systech_account.User.companies: systech_account.Company

每次我尝试

python manage.py makemigrations

在我的 (Ubuntu) 中。但是当我尝试在我的 Windows 和我的同事 (Ubuntu) 上 运行 它时,它工作正常 。 (*我们通过 Git repo 共享项目)。他们可以成功地进行迁移。我们想知道为什么只有我的 PC 会发生这种情况。

备注:

我们尝试过的解决方案:


  1. 我们如何解决这个问题?

谢谢! :)

我在这里猜测 systech_account.Company 是一个应用程序中的模型,该模型尚未加载,同时试图为另一个应用程序创建迁移。

如果这是真的,它可能首先可以解决 运行

makemigrations app_that_has_company_in_it

在 运行 你的另一个 makemigrations 之前。

但这不是解决方案。

您和您的同事都在尝试执行相同的迁移这一事实告诉我,您没有将迁移存储在源代码管理中,这才是真正的问题所在。

这样做 确实 阻止了这些错误的发生(至少 me/us)并且在尝试进行迁移时会更快,因为没有任何重复步骤。如果你担心与这些合并冲突,你不必担心,django 非常聪明。