Search code examples
pythondjangosyntaxmanytomanyfield

Django referencing a ManyToManyField within the same model error without quotations


https://docs.djangoproject.com/en/1.11/topics/db/examples/many_to_many/

So I based my code off this document and I hit an error.

from django.db import models

class Gift(models.Model):
    name = models.CharField(
      max_length=120, unique=True, help_text='Enter the gift item name'
    )
    # skipping to the ManyToMany ....
    genre = models.ManyToManyField(Category)

class Category(models.Model):
    # skipping stuff like CATEGORIES ...
    type = models.IntegerField(
      primary_key   = True, choices = CATEGORIES, default = EVENT,
    )
    description = models.CharField(max_length=500, null=True, blank=True)

I receive this error

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/.../gifting_db/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/.../gifting_db/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
    django.setup()
  File "/.../gifting_db/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/.../gifting_db/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/.../gifting_db/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/.../gifting_db/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/.../gifting_db/gifting/gifts/models.py", line 5, in <module>
    class Gift(models.Model):
  File "/.../gifting_db/gifting/gifts/models.py", line 20, in Gift
    category    = models.ManyToManyField(Category)
NameError: name 'Category' is not defined

But if I add quotations to the 'Category' it works. Any idea why? The django doc example does not use quotations https://docs.djangoproject.com/en/1.11/topics/db/examples/many_to_many/ between the 'Publication' and 'Article'. If anything, I would guess I'm putting 'Category' in as a string, not as a reference to the Category object?

genre = models.ManyToManyField('Category')

Solution

  • Your problem is that Category is not defined until after Gift. If you want to use it without quotes you need to place Category above Gift. Otherwise, quotations work fine and provide the same functionality.

    https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

    If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself: