1

I am new to Django & Python and have been following a tutorial, but I have an error that has me stumped.

I am attempting to build my django models / database.

When I attempt to run python manage.py syncdb I receive the following error in my command line prompt:

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>

This is a cut down version of my models.py file (lines 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"

The relationship between LanguageDirection & LangaugeCode is a one-to-many, where LanguageDirection.LanguageDirectionID (one) and LanguageCode.LanguageDirectionID (many).

Any suggestions as to what is causing this error and how I can fix this?

3 Answers 3

4

This is the meat of the error:

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

The line should be:

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

Since the ForeignKey takes a class, or the name of a class in quotes such as "LanguageDirection".

NameError, for the future, means you're attempting to use a variable that is either not in scope, or does not exist.

Sign up to request clarification or add additional context in comments.

Comments

2

Something that happened to me a few times is that when defining the models in Django, the Foreign key field relates to a model that´s defined further down in the same document.

This gives me no warnings in Pycharm 2.7 which I consider somewhat odd. Anyways, to solve this just put '' around the class name.

models.ForeignKey('SomeModel')

Comments

1

languageDirectionID = models.ForeignKey(languageDirection, default=1, db_column="languageDirectionID") should be languageDirectionID = models.ForeignKey(LanguageDirection, default=1, db_column="languageDirectionID"). The error refers to the issue that languageDirection, which should refer to the class name and not the DB table name for the foreign key target, is not capitalized correctly.

1 Comment

Thank you! I should have know better. What a stupid mistake by me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.