Django:名称错误:名称“类别”没有定义

发布于 2024-09-07 05:01:23 字数 345 浏览 1 评论 0原文

我正在练习 Django 并使用一些在线教程来构建一个网络博客。第一个项目进展顺利,但是当我尝试第二个项目时,通过开发第一个视图,出现了这样的语句:

categories = models.ManyToManyField(Category, related_name ="packages")

在教程中,验证模型给出了 0 个错误,但是当我运行验证命令时它给出了我:名称错误:名称“类别”未定义

我三次检查了所有文件的语法,没有任何语法错误,教程中没有提到其他导入。

怎么了?

I'm practicing on Django and using some online tutorial to build a web blog. It went smoothly with the first project, yet when I tried the 2nd one, through developing the first view, there was this statement:

categories = models.ManyToManyField(Category, related_name ="packages")

In the tutorial, validating the model gives 0 errors, yet when I ran validating command it gave me :NameError: name 'Category' is not defined

I triple checked the syntax of all the file and there was no single syntax error, there is no additional imports mentioned in the tutorial.

What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

刘备忘录 2024-09-14 05:01:23

听起来你可能是 Python 新手,因为你说“我三次检查了所有文件的语法,没有任何语法错误,教程中没有提到额外的导入。”

请注意,在 Python 中,许多在 C++ 等语言中在编译时捕获的与名称相关的错误会在运行时捕获。当我开始使用 Python 时,这让我有点困惑。运行时错误可能只是表明仅仅是拼写错误等(如果这听起来很糟糕,请不要担心 - 有免费的工具可以检查您的程序作为编译器是否有像其他语言那样的东西)

是否在该行出现在同一个文件中?

如果没有,您必须专门导入名称类别。假设 Category 是在另一个文件 argh.py 中定义的。

import argh

不行。

您需要这样做

from argh import Category

(或者更改您的代码以引用 argh.Category)

无论如何,除非您提供更多信息,否则您的问题实际上无法得到更好的回答。那条线不是问题。问题可能出在类别定义上。类别的定义在哪里? Category 是另一个模型类。应该有类似的代码

class Category:
     #....

,并且它应该位于同一个文件中,或者如果它位于名为“prison.py”的文件中,那么您正在处理的文件应该包含

from prison import Category

It sounds like you may be new to Python, since you say "I triple checked the syntax of all the file and there was no single syntax error, there is no additional imports mentioned in the tutorial."

Be aware that in Python, many name-related errors that would be caught at compile time in languages like C++ are caught at runtime instead. This tripped me up a little when I started using Python. Runtime errors could simply be indicating a mere typo, etc. (If this sounds nasty, don't worry-- there's free tools out there to check your program as a compiler for stuff like that some other languages would)

Is Category defined in the same file that that line appears in?

If not, you must specifically import the name Category. Suppose Category is defined in another file, argh.py.

import argh

is no good.

You would need to do

from argh import Category

(or alternatively change your code to reference argh.Category)

Anyway, your question can't really be answered better than that unless you give more information. That line isn't the problem. The problem is probably with the Category definition. Where is the definition of Category? Category is another model class. There should be code like

class Category:
     #....

And it should either be in the same file, or if it's in a file called "prison.py" then the file you are working on should contain

from prison import Category
梦在深巷 2024-09-14 05:01:23

链接到“某些”会很有用。

但是,问题是您尚未导入类别或您使用提到的键在模型下定义类别。

It would be useful to link on "some".

However, problem is that You have not imported Category or You define Category under the model with the key mentioned.

白首有我共你 2024-09-14 05:01:23

我得到了同样的错误如下:

名称错误:名称“类别”未定义

因为我将 Category 设置为 ForeignKey()Category 类之前定义,如下所示:

class Product(models.Model):     # Error
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

class Category(models.Model):
    name = models.CharField(max_length=50)

因此,我将 Category 设置为 ForeignKey() 在 Category 类定义如下,然后错误得到解决:

class Category(models.Model):
    name = models.CharField(max_length=50)

class Product(models.Model):     # Here
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

或者,我将带引号的 Category 设置为 ForeignKey() 作为如下所示,则错误已解决:

class Product(models.Model):   # ↓  Here  ↓
    category = models.ForeignKey("Category", on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

class Category(models.Model):
    name = models.CharField(max_length=50)

*您可以看到 我的问题和答案解释了 Django 模型字段中带或不带引号的类名具有外键关系。

I got the same error below:

NameError: name 'Category' is not defined

Because I set Category to ForeignKey() before Category class is defined as shown below:

class Product(models.Model):     # Error
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

class Category(models.Model):
    name = models.CharField(max_length=50)

So, I set Category to ForeignKey() after Category class is defined as shown below, then the error was solved:

class Category(models.Model):
    name = models.CharField(max_length=50)

class Product(models.Model):     # Here
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

Or, I set Category with quotes to ForeignKey() as shown below, then the error was solved:

class Product(models.Model):   # ↓  Here  ↓
    category = models.ForeignKey("Category", on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

class Category(models.Model):
    name = models.CharField(max_length=50)

*You can see my question and the answer explaining the class name with or without quotes in a Django model field to have foreign key relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文