Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 365
Rahul Satal Avatar asked Mar 11 '26 01:03

Rahul Satal


1 Answers

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 I was getting the error even after undoing the changes.

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

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

like image 95
Rahul Satal Avatar answered Mar 13 '26 16:03

Rahul Satal