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

  • SEARCH
  • Home
  • 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 8128473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:53:43+00:00 2026-06-06T07:53:43+00:00

I am trying to expand the relationships within my Django Models. I have a

  • 0

I am trying to expand the relationships within my Django Models. I have a system where elements are stored within Categories. How do I structure my models.py so that each category is related to a subcategory?

Here is what my category model looks like:

class Category(models.Model):
   site = models.ForeignKey(Site)
   template_prefix = models.CharField(max_length=200, blank=True)
   name = models.CharField(max_length=200)
   slug = models.SlugField() 
   description = models.TextField(default='')
   sortby_fields = models.CharField(max_length=200,
                                 help_text=_(u'A comma separated list of field names that should show up as sorting options.'),
                                 blank=True)
   sort_order = models.PositiveIntegerField(default=0)

   def __unicode__(self):
       return self.name + u' Category'

   class Meta:
       verbose_name_plural = u'categories'

Thanks for any suggestions.

  • 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-06T07:53:44+00:00Added an answer on June 6, 2026 at 7:53 am

    You can create a foreign key to itself:

    class Category(models.Model):
       ...
       parent_category = models.ForeignKey('self', null=True, blank=True)
    

    Then you can assigning any existing Category instance as the parent_category of that instance. Then, if you wanted to find all of the subcategories of a given Category instance you would do something like:

    subcategories = Category.objects.filter(
        parent_category__id=target_category.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table column I’m trying to expand and hide. jQuery seems to
I am trying to make my DIV's height expand to it's child elements. I
I'm currently trying to expand my basic news feature so that comments can be
Im trying to have a layer animate/expand hight on click of a show button,
I'm trying to build a strong database that's normalized and has room to expand
I'm trying to expand navigation options of the context menu on certain elements (specifically,
I am trying to expand from php. Here is a basic problem I have
We have a Spring 3 MVC webapplication, and we are trying to expand it
I am trying to expand on a basic hello world application for the google
I'm currently trying to expand my PHP driven intranet site for my company. It

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.