ValueError: Related model u'mutech.branch' cannot be resolved

ValueError: Related model u'mutech.branch' cannot be resolved

我正在尝试在我的 models.py 文件中创建外键。但是在 运行 python manage.py 迁移命令上我得到了以下错误,之前一切都很好。即使我撤消了所有更改,它仍然会出现相同的错误,我也尝试删除我的数据库,但没有任何效果。

          Applying mutech.0004_sub_branch...Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/home/rahul/mydjangoapp/jango/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
       .
       .
       .
       .
       .

      File "/home/rahul/mydjangoapp/jango/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1414, in resolve_related_fields
        raise ValueError('Related model %r cannot be resolved' % self.rel.to)
    ValueError: Related model u'mutech.branch' cannot be resolved

models.py file-

from django.db import models

class branch(models.Model):
    branch_title = models.CharField(max_length=50)

    def __unicode__(self):              # __str__ on Python 3
            return str(self.branch_title)   

class project(models.Model):
    project_title = models.CharField(max_length=50)
    project_image = models.ImageField(upload_to="images")
    project_desc = models.CharField(max_length=200)
    project_duration = models.CharField(max_length=50)
    branch = models.ForeignKey(branch)

    def __unicode__(self):              # __unicode__ on Python 2
            return str(self.project_title)

view.py file is -

from django.shortcuts import render, get_object_or_404, render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from mutech.models import *

def project_info(request):
    project_list = project.objects.all()
    branch_list = branch.objects.all()
    context = {'project_list':project_list , 'branch_list':branch_list }
    return render(request, 'mutech/project.html', context)

def project_branch_info(request):
    branch_list = branch.objects.all()
    context = {'branch_list':branch_list }
    return render(request, 'mutech/project_branch_info.html', context)

对我有用的解决方案是完全删除我的迁移文件夹和数据库 运行 按照命令-

python manage.py makemigrations

python manage.py migrate

因为这个错误发生在我身上是由于外键的一些错位,即使在撤消更改后我仍然收到错误。

我们正在删除应用程序中的迁移文件夹,因为实际问题出在该文件夹上,迁移文件夹中没有任何特殊之处,它将使用您的 model.py 文件 运行 命令重新创建-python manage.py makemigrations。解决办法就是把Migration文件夹删掉,用命令重新创建。

那么你要做的-

  1. 从应用程序中删除迁移文件夹。
  2. 运行 命令 python manage.py makemigrations 然后 python manage.py迁移

Caution: The data in the database will be lost after this, So perform this only if your data is not important.

我是这样解决这个问题的:

  1. 为您的相关模型创建一个新的 ForeignKey 调用 tmp。 运行 迁移。
  2. 删除旧的 ForeignKey 和 运行 迁移。
  3. tmp 重命名为旧 ForeignKey 的名称。 运行 迁移。

所以,您最终需要三个迁移文件来做一件事,但至少它做到了!

此问题是由您的迁移中的循环依赖引起的。在最新迁移之前 运行 的其他一些迁移正在调用一个恢复模型重命名之前状态的迁移。例如,您在 xyzzy.migrations.0004_rename 中将模型 foo 重命名为 bar,但在应用迁移期间,在 xyzzy.migrations.0004_rename 运行s bozotic.0002_bozo 之后依赖于 xyzzy.migrations.0001_initial 因此现在必须应用的迁移看不到 xyzzy.migrations.004_rename.

创建的状态

我花了一个小时调试并修复了我项目中的这个错误。

检查您的迁移依赖关系并尝试手动调整依赖关系,以便 运行迁移模型处于所需状态