Django在相同的模型错误中引用ManyToManyField而没有引号

时间:2017-07-29 15:21:08

标签: python django syntax manytomanyfield

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

所以我将我的代码基于此文档,我遇到了错误。

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)

我收到此错误

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

但是,如果我在“类别”中添加引用,它就可以了。知道为什么吗? django doc示例在“Publication”和“Article”之间不使用引用https://docs.djangoproject.com/en/1.11/topics/db/examples/many_to_many/。如果有的话,我猜我会把'Category'作为一个字符串,而不是作为Category对象的引用?

genre = models.ManyToManyField('Category')

2 个答案:

答案 0 :(得分:1)

您需要在Category之前定义Gift或使用引号。

from django.db import models


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)


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)

use quotes

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

答案 1 :(得分:0)

您的问题是在Category之后才定义Gift。如果您想在没有引号的情况下使用它,则需要将Category放在Gift之上。否则,报价可以正常工作并提供相同的功能。

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

  

如果您需要在尚未使用的模型上创建关系   定义后,您可以使用模型的名称,而不是模型   对象本身: