2017-07-29 56 views
1

https://docs.djangoproject.com/en/1.11/topics/db/examples/many_to_many/Django的相同型號的誤差範圍內引用ManyToManyField中沒有報價

所以,我根據我的代碼關閉這個文件和我打了一個錯誤。

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示例在「發佈」和「文章」之間不使用引用https://docs.djangoproject.com/en/1.11/topics/db/examples/many_to_many/。如果有的話,我猜想我把'Category'放在一個字符串中,而不是對Category對象的引用?

genre = models.ManyToManyField('Category') 

回答

0

您的問題是Category是直到Gift之後定義。如果你想使用它沒有引號,你需要將Category放在Gift以上。否則,報價工作正常,並提供相同的功能。

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

如果你需要創建一個尚未被 定義的模型的關係,您可以使用該模型的名稱,而不是模型 對象本身:

+0

啊,這就是爲什麼它的報價工作。真棒謝謝你,我錯過了在外鍵部分。 – myaddresstoday

1

您需要在Gift之前定義Category或使用引號。

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