Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:26:09+00:00 2026-06-11T18:26:09+00:00

So, i have posts, and category: class Post(models.Model): … category = models.ForeignKey(Category) def __unicode__(self):

  • 0

So, i have posts, and category:

class Post(models.Model):
    ...
    category = models.ForeignKey(Category)

    def __unicode__(self):
        return self.title

class Category(models.Model):
    category = models.CharField(max_length = 30, unique=True)
    id_post = models.ForeignKey(Post)   

    def __unicode__(self):
        return self.category

i write

python manage.py validate

and NameError: Name Category is not defined. WHY???

i`m use sqllite, thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:26:10+00:00Added an answer on June 11, 2026 at 6:26 pm

    Place Category above Post in models.py. Django / Python validates the models from top to down. I also stumbled over it when beginning with Django 🙂

    class Category(models.Model):
        category = models.CharField(max_length = 30, unique=True)   
    
        def __unicode__(self):
            return self.category
    
    class Post(models.Model):
        ...
        category = models.ForeignKey(Category)
    
        def __unicode__(self):
            return self.title
    

    As you placed in your source code a relationship from Post to Category you probably intended to have a relationship from a category instance to all related post instances. This is build-in in Django and you can reverse ForeignKey relationships using the ‘modelname_set’ attribute.

    So to get all posts which are assigned to a specific Category you can do:

    myCategory =Category.objects.get(pk=1)
    myCategory.post_set.all()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Model in django : class Post(models.Model): title = models.CharField(max_length=255) category =
I have two models with managers: Category and Project. class PopulatedCategoriesManager(models.Manager): def get_query_set(self): return
I have 2 models, Post and Category: class Category(models.Model): # some fields class Post(models.Model):
I have two models, Category and Post. Category.rb class Category include Mongoid::Document field :title,
I have the following models: Post.rb class Post < ActiveRecord::Base belongs_to :category has_many :attachments,
I have three models: category.rb class Category include Mongoid::Document # Relationships has_many :posts, :autosave
I have a function that looks like this: def post_count(self): return self.thread_set.aggregate(num_posts=Count('post'))['num_posts'] I only
If I have a model... class Post include Mongoid::Document field :link field :title field
I have 2 tables - posts and postmeta. posts ID title category post_status post_type
If I have 3 models... Post, Logo_Category and Logo and 5 tables posts, logos,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.