Django - models.ForeignKey 给出提到 class 的错误未定义

Django - models.ForeignKey Giving error that mentions class is not defined

我正在用 django 开发一个学生管理系统,在制作它的过程中我得到了这个

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

并且在进行迁移时出现此错误 我可以通过什么方式解决这个问题?我该如何解决这个错误

    (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>

老实说,我不知道您为什么会收到此错误,因为您在示例中显示的内容是正确的。唯一一次(至少我知道)你得到这个错误的时间是:

  1. 两个模型定义在不同的文件中,导入关系模型有问题

  2. 创建关系的模型在关系之后定义。在您的示例中,这意味着在 Course 模型之后定义 Dept 模型。

您可以使用 APP_NAME.MODEL_NAME 将模型作为字符串导入,这样可以避免这种情况,特别是我描述的第二种情况。

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