Django syncdb - NameError:未定义名称'*********'

时间:2012-05-03 22:53:09

标签: django django-models

我是Django&的新手。 Python并且一直在关注一个教程,但是我遇到了一个让我难过的错误。

我正在尝试构建我的django模型/数据库。

当我尝试运行 python manage.py syncdb 时,我在命令行提示符下收到以下错误:

C:\Users\6233114\Django-Projects\GlobalX>python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
443, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 196,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 231,
 in execute
    self.validate()
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 266,
 in validate
    num_errors = get_validation_errors(s, app)
  File "C:\Python27\lib\site-packages\django\core\management\validation.py", lin
e 30, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 158, in
 get_app_errors
    self._populate()
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 64, in
_populate
    self.load_app(app_name, True)
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 88, in
load_app
    models = import_module('.models', app_name)
  File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
    __import__(name)
  File "C:\Users\6233114\Django-Projects\GlobalX\app_data\models.py", line 15, i
n <module>
    class LanguageCode(models.Model):
  File "C:\Users\6233114\Django-Projects\GlobalX\app_data\models.py", line 27, i
n LanguageCode
    languageDirectionID = models.ForeignKey(languageDirection, default=1, db_col
umn="languageDirectionID")
NameError: name 'languageDirection' is not defined

C:\Users\6233114\Django-Projects\GlobalX>

这是我的models.py文件的缩减版本(第1 - 29行):

    from django.db import models
    from django.contrib.auth.models import User

    class LanguageDirection(models.Model):
      languageDirectionID = models.AutoField(primary_key=True, db_column="languageDirectionID")
      languageDirectionDescription = models.CharField(max_length=20, db_column="languageDirectionDescription")
      languageDirDescription = models.CharField(max_length=20, db_column="languageDirDescription")
      textAlign = models.CharField(max_length=20, db_column="textAlign")
      oppositeLanguageDirectionDescription = models.CharField(max_length=20, db_column="oppositeLanguageDirectionDescription")
      oppositeLanguageDirDescription = models.CharField(max_length=20, db_column="oppositeLanguageDirDescription")
      oppositeTextAlign = models.CharField(max_length=20, db_column="oppositeTextAlign")
      class Meta:
        db_table="languageDirection"

    class LanguageCode(models.Model):
      languagecodeID = models.AutoField(primary_key=True, db_column="languageCodeID")
      languageCodeDescription = models.CharField(max_length=10, db_column="languageCodeDescription")
      baseLanguageCode = models.CharField(max_length=10, db_column="baseLanguageCode")
      languageNameEng = models.CharField(max_length=255, db_column="languageNameEng")
      altLanguageNameEng = models.CharField(max_length=255, blank=True, null=True, db_column="altLanguageNameEng")
      languageNameNative = models.CharField(max_length=255, db_column="languageNameNative")
      altLanguageNameNative = models.CharField(max_length=255, blank=True, null=True, db_column="altLanguageNameNative")
      iso639_1 = models.CharField(max_length=10, blank=True, null=True, db_column="iso639_1")
      iso639_2T = models.CharField(max_length=10, db_column="iso639_2T")
      iso639_2B = models.CharField(max_length=10, db_column="iso639_2B")
      iso639_X = models.CharField(max_length=10, db_column="iso639_X")
      languageDirectionID = models.ForeignKey(languageDirection, default=1, db_column="languageDirectionID")
      class Meta:
        db_table="languageCode"

LanguageDirection&amp;的关系LangaugeCode是一对多的,其中LanguageDirection.LanguageDirectionID(一个)和LanguageCode.LanguageDirectionID(很多)。

有关导致此错误的原因以及我如何解决此问题的任何建议?

3 个答案:

答案 0 :(得分:4)

这是错误的结果:

languageDirectionID = models.ForeignKey(languageDirection, default=1, db_col
umn="languageDirectionID")
NameError: name 'languageDirection' is not defined

该行应为:

languageDirectionID = models.ForeignKey(LanguageDirection, default=1, db_column="languageDirectionID")

由于ForeignKey使用引号(例如"LanguageDirection")中的类或类的名称。

NameError,对于将来,意味着您正在尝试使用不在范围内或不存在的变量。

答案 1 :(得分:2)

发生在我身上的事情是,在Django中定义模型时,外键字段与在同一文档中进一步定义的模型有关。

这让我在Pycharm 2.7中没有任何警告,我认为这有点奇怪。无论如何,解决这个问题只是把''围绕着班级名称。

models.ForeignKey('SomeModel')

答案 2 :(得分:1)

languageDirectionID = models.ForeignKey(languageDirection, default=1, db_column="languageDirectionID")应为languageDirectionID = models.ForeignKey(LanguageDirection, default=1, db_column="languageDirectionID")。该错误指的是languageDirection(应该引用类名而不是外键目标的DB表名)未正确大写的问题。