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 7592719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:58:46+00:00 2026-05-30T20:58:46+00:00

I have a simple category model: class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField()

  • 0

I have a simple category model:

class Category(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField()
    parent = models.ForeignKey('self', blank = True, null = True, related_name="children")

At first, my data seemed to need only categories and subcategories, but I realized that there are some cases where I still want to sub-sub-categorize.

I want my urls to be category/subcategory/sub-subcategory

I was thinking about how to implement this but I am not sure, since my url pattern matching looks like this:

url(r'^business/(?P<parent>[-\w]+)/(?P<category_name>[-\w]+)$', 'directory.views.show_category'),

Basically only allowing a single sub-category, since my view method accepts these two parameters.

What is the best way to handle this?

  • 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-05-30T20:58:47+00:00Added an answer on May 30, 2026 at 8:58 pm

    What about unlimited levels? At urls.py:

    url(r'^business/(?P<hierarchy>.+)/', 'directory.views.show_category')
    

    And at directory/views.py:

    def show_category(request, hierarchy):
        category_slugs = hierarchy.split('/')
        categories = []
        for slug in category_slugs:
            if not categories:
                parent = None
            else:
                parent = categories[-1]
            category = get_object_or_404(Category, slug=slug, parent=parent)
            categories.append(category)
        ...
    

    Don’t forget to add unique_together = ('slug', 'parent',) to Category.Meta, otherwise you are doomed.

    [update]

    could I just query the db with category_slugs[-1] and if the obtained category has no children, we know its a leaf category, otherwise, we know it has subcategories and we show them? – alexBrand

    @alexBrand: consider the following hypothetical URLs:

    /business/manufacture/frozen/pizza/
    /business/restaurant/italian/pizza/
    /business/delivery-only/italian/pizza/
    /business/sports/eating-contest/pizza/
    

    If you think such scenario is possible, then IMHO a simpler test (without the whole hierarchy) is not enough.

    What are your real concerns regarding the proposed solution? At the end of the loop, the variable category will hold the correct category_slugs[-1], and you will have the whole hierarchy available in categories. Don’t worry about performance, my best advice is: don’t try to optimize an elegant solution before profiling (you will be surprised).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model with news and categories: class Category(models.Model): name = models.CharField()
I have a model that looks like this: class Category(models.Model): name = models.CharField(max_length=60) class
I have this model: class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent =
I'm looking for a simple modelformset example - I have a model class Category(BaseModel):
Given class Category(db.Model): name = db.Stringproperty() Say I have a nested hierarchy -root |-a
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a Model Category which has name:string categoryId:integer parent_id:integer the categoryId doesnt matter
I have a simple Core Data model with two string attributes (size and category).
I have a simple Crud application where there is one filed called Category. During
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input

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.