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?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
ma2moun
  • 80
  • 1
  • 9
  • I would probably suggest that Category is not defined. Did you call it Catergory by mistake in the class? – Lee Jun 27 '10 at 22:47

3 Answers3

4

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
Domingo Ignacio
  • 1,194
  • 7
  • 13
  • Thanks both, Not new but in fact I missed something in the code where I should define the class Category, I was following a tutorial and I thought that Category is included within the tutorial package, defined the class and the problem solved. Thanks – ma2moun Jun 28 '10 at 08:48
  • 1
    So, is your problem solved? Did you find these answers helpful? – Domingo Ignacio Jun 29 '10 at 19:23
  • I came across sth which was similar to Pascal language? is defining the class above or below the main class different? I define the Category class below my main class it raised error, but when I cut and paste it above the main one, it was o.k. so it doesn't parse the whole file? – HesamDadafarin Oct 01 '14 at 14:23
  • Hesam, python executes the code in the order it is given. This does not mean you cannot refer to values defined later from code defined (but not executed) earlier, however you cannot refer to values defined later from code that is defined _and_ executed. For example, if the line using category had been inside a function definition, it would have been OK, since this only defines a function but does not yet execute the code inside the function till later, when the name was already defined. Make sense? (Also, I don't know Pascal and am not sure what you mean by the Pascal reference.) – Domingo Ignacio Oct 02 '14 at 20:13
0

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.

Almad
  • 5,753
  • 7
  • 35
  • 53
0

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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129