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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:05:27+00:00 2026-05-29T04:05:27+00:00

In my project I am using django-mptt for categories. My model: class Category(models.model): name

  • 0

In my project I am using django-mptt for categories.

My model:

class Category(models.model):
    name = models.CharField()
    parent = models.ForeignKey("self", blank=True, null=True,
                           related_name="sub_category")
    nav_order = models.IntegerField(null=False, blank=False, default=0)
    # unsure need nav_order column in DB

    class Meta:
        verbose_name_plural = 'Categories'
mptt.register(Category)

And I need to have ability get order for current category like this:

Category                Navigation order(one column)

CatA                      0
|-subcat11                 0
  |-sub11a                    0
  |-sub11b                    1
  \-sub11c                    2
\-subcat12                 1
CatB                      1  
|-subcat21                 0
|-subcat22                 1
\-subcat23                 2
  \-sub23a                    0
CatC                      2

How can I quickly fill/recalculate order column on creating/moving elements.
Or calculate it by Category’s method Category.objects.get(name='sub11b').get_order() should return 1.

  • 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-29T04:05:28+00:00Added an answer on May 29, 2026 at 4:05 am

    When defining the model you can specify the ordering with “order_insertion_by”.

    So something like this:

    class Category(MPTTModel):
        name = models.CharField()
        parent = models.ForeignKey("self", blank=True, null=True, 
                 related_name="sub_category")
    
        class MPTTMeta:
            order_insertion_by = ['name']
    

    Then you can rebuild your database with Category.tree.rebuild() which should respect the ordering specified.

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

Sidebar

Related Questions

Using Django 1.1.1 In models.py: class Site(models.Model): name = models.CharField(max_length=50) class SiteMonth(models.Model): site =
I'm using django-mptt for a Category model, which is a foreign key to a
I've been using django-mptt in my project for a while now, it's fabulous. Recently,
I'm building a small web project using Django that has one model ( Image
I need some opinions here. I'm working on a Django project using buildout to
I'm considering using Django for a project I'm starting (fyi, a browser-based game) and
I am using django 1.1 beta release. In my project I want to use
In my Django project I am using Product.objects.all().order_by('order') in a view, but it doesn't
I've been using Komodo Edit for a small project in Django. The code completion
I am working on a Silverlight project that uses Django on the server using

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.