Home > Software engineering >  Django - models.ForeignKey Giving error that mentions class is not defined
Django - models.ForeignKey Giving error that mentions class is not defined

Time:02-25

I am developing a Student management system in django and while making it I got this

class Dept(models.Model):
id = models.CharField(primary_key='True', max_length=100)
name = models.CharField(max_length=200)

def __str__(self):
    return self.name


class Course(models.Model):
    dept = models.ForeignKey(Dept, on_delete=models.CASCADE)
    id = models.CharField(primary_key='True', max_length=50)
    name = models.CharField(max_length=50)
    shortname = models.CharField(max_length=50, default='X')

    def __str__(self):
        return self.name

and while doing make migrations I get this error by what means Can I get this right and how can I tackle this error

    (studentmanagementsystem) C:\Users\harsh\dev\student management\student_management>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\harsh\anaconda3\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\harsh\anaconda3\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\harsh\anaconda3\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\harsh\anaconda3\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Users\harsh\anaconda3\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\harsh\anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\harsh\dev\student management\student_management\student_app\models.py", line 10, in <module>
    class Dept(models.Model):
  File "C:\Users\harsh\dev\student management\student_management\student_app\models.py", line 18, in Dept
    class Course(models.Model):
  File "C:\Users\harsh\dev\student management\student_management\student_app\models.py", line 19, in Course
    dept = models.ForeignKey(Dept, on_delete=models.CASCADE)
NameError: name 'Dept' is not defined

(studentmanagementsystem) C:\Users\harsh\dev\student management\student_management>

CodePudding user response:

Honestly, I do not know why you were getting this error as what you have shown here in the example is correct. The only times (at least that I know of) you get this error is when:

  1. The two models are defined in different files and there is a problem with importing the relationship model.

  2. The model that the relationship is being created with is defined after the relationship. In your example, it would mean defining the Dept model after the Course model.

You can import your models as a string using APP_NAME.MODEL_NAME, which avoids such cases, specially the second scenario I have described.

class Course(models.Model):
    # Assuming your model resides in an app called 'student_app'
    dept = models.ForeignKey('student_app.Dept', on_delete=models.CASCADE)
  • Related