ValueError:相关模型u'mutech.branch'无法解决 [英] ValueError: Related model u'mutech.branch' cannot be resolved

查看:251
本文介绍了ValueError:相关模型u'mutech.branch'无法解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的models.py文件中创建外键。但是在运行python manage.py migrate命令时,我得到以下错误,以前每件事情都是好的。即使我已经撤消所有的修改,它仍然给出相同的错误,我也尝试删除我的数据库,但没有任何工作。

 应用mutech .0004_sub_branch ... Traceback(最近的最后一次调用):
文件manage.py,第10行,< module>
execute_from_command_line(sys.argv)
文件/home/rahul/mydjangoapp/jango/local/lib/python2.7/site-packages/django/core/management/__init__.py,第385行,在execute_from_command_line
utility.execute()






文件/home/rahul/mydjangoapp/jango/local/lib/python2.7/site-packages/django/db/models/fields/related.py,第1414行,在resolve_related_fields
raise ValueError('相关模型%r无法解析'%self.rel.to)
ValueError:相关模型u'mutech.branch'无法解析




models.py文件 -




$ d

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

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

类项目(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(b牧场)

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




view.py文件是 -




从django.shortcuts导入render,get_object_or_404,render_to_response 
从django.http导入HttpResponse,HttpResponseRedirect
从mutech.models import *

def project_info(请求)
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文件夹,并使用命令重新创建它。



所以你要做的 -



<
  • 从应用程序中删除迁移文件夹。

  • 运行命令 python manage.py makemigrations ,然后执行python管理。 py migrate


  • I am trying to make foreign key in my models.py file. But on running python manage.py migrate command i got the below error, previously every thing was fine. Even i have undo all my changes it still giving same error,I have also tried deleting my database but nothing works.

              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)
    

    解决方案

    The solution which worked for me is to delete my migrations folder and database completely thereafter running following commands-

    python manage.py makemigrations

    python manage.py migrate

    because this error occured to me due to some misplacement of foreign key, and even after undo , this error was not going.

    We are deleting the migration folder in the app because the actual problem is with that folder and there is nothing special in migration folder and it will be recreated using your model.py file running the command -python manage.py makemigrations. The solution is just to delete the Migration folder and recreate it using commands.

    So what you have to do-

    1. Delete migration folder from the app.
    2. Run the commands python manage.py makemigrations and then python manage.py migrate

    这篇关于ValueError:相关模型u'mutech.branch'无法解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆