2015-04-12 64 views
3

我想在我的models.py文件中創建外鍵。但是在運行python manage.py migrate命令時,我得到了下面的錯誤,以前每一件事情都很好。即使我已經撤消了所有的改變,它仍然給出同樣的錯誤,我也嘗試刪除我的數據庫,但沒有任何工作。ValueError:相關模型u'mutech.branch'無法解析

  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) 
+0

檢查,如果存在與名稱分支另一VAR /手動模式調整嘗試依賴,還,U應當使用大寫字母。科。 – levi

+0

感謝您的回答,但model.py文件中只有一個分支類別 –

+0

您可以發佈您的遷移文件嗎? – knbk

回答

6

這爲我工作的解決方案是完全刪除我的文件夾遷移和數據庫之後運行以下命令 -

python manage.py makemigrations

python manage.py migrate

,因爲這個錯誤發生在我身上,由於外鍵的錯位,甚至在撤消之後,這個錯誤不會發生。

我們正在刪除應用程序中的遷移文件夾,因爲實際的問題與該文件夾有關,遷移文件夾中沒有任何特別的地方,它將使用運行命令的model.py文件重新創建 - python manage.py makemigrations。解決方案只是刪除Migration文件夾並使用命令重新創建它。

所以,你必須從應用程序做 -

  1. 刪除遷移文件夾的內容。
  2. 運行命令蟒蛇manage.py makemigrations然後蟒蛇manage.py遷移

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

+0

當我嘗試這個時,它迴應了「python manage.py makemigrations」和「未檢測到變化」。 – rossdavidh

+0

你好@rossdavidh!我想你沒有刪除遷移文件夾,所以先刪除它,然後再次嘗試「python manage.py makemigrations」命令。如果它仍然不起作用,那麼也嘗試刪除遷移文件夾和數據庫,然後運行命令。 –

-1

我想這是在Django 1.7中的錯誤(如果你使用它)。

看來,如果您刪除以前的遷移並執行新的遷移,它將起作用。另外,我建議你寫

branch = models.ForeignKey('branch') 

而不是

branch = models.ForeignKey(branch) 
+2

這是一個相當糟糕的建議。字符串引用可以隱藏所有可能的錯誤,並且只能用於防止循環導入。 – knbk

+0

hello @Rafael Cardoso我嘗試過使用'branch'。但它仍然給出同樣的錯誤。 –

1

這是我如何解決了這個問題:

  1. 創建一個新的ForeignKey通話tmp到您的相關模型。運行遷移。
  2. 刪除舊的ForeignKey並運行遷移。
  3. tmp重命名爲舊的ForeignKey被調用。運行遷移。

所以,你最終有三個遷移文件做一件事,但至少它做到了!

+0

遷移後不會導致數據丟失嗎? – texnic

+0

@texnic它爲我工作。你的里程可能會有所不同......⍨ – Sardathrion

0

此問題是由您的遷移中的循環依賴引起的。在最新遷移之前運行的其他一些遷移是調用遷移,以恢復模型重命名之前的狀態。例如,你在xyzzy.migrations.0004_rename但在應用遷移改名模型foobar,後xyzzy.migrations.0004_rename運行bozotic.0002_bozo取決於xyzzy.migrations.0001_initial因此有現在被應用並沒有看到xyzzy.migrations.004_rename創建的狀態遷移。

花了一個小時在我的項目中調試和修復這個錯誤。

檢查遷移依賴關係,並讓運行時遷移模型是在理想狀態