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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:22:21+00:00 2026-05-14T18:22:21+00:00

I’m having some trouble figuring out the best/Djangoic way to do this. I’m creating

  • 0

I’m having some trouble figuring out the best/Djangoic way to do this. I’m creating something like an interactive textbook. It has modules, which are more or less like chapters. Each module page needs to list the topics in that module, grouped into sections.

My question is how I can ensure that they list in the correct order in the template? Specifically:

1) How to ensure the sections appear in the correct order?
2) How to ensure the topics appear in the correct order in the section?

I imagine I could add a field to each model purely for the sake of ordering, but the problem with that is that a topic might appear in different modules, and in whatever section they are in there they would again have to be ordered somehow.

I would probably give up and do it all manually were it not for the fact that I need to have the Topic as object in the template (or view) so I can mark it up according to how the user has labeled it.

So I suppose my question is really to do with whether I should create the contents pages manually, or whether there is a way of ordering the query results in a way I haven’t thought of. Thanks for your help!

edit: The simplest thing I can think of would be if I could have an ordered list (of topics) in the module or section model, but afaik that’s not possible. Is it? Perhaps with a custom field?

  • 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-14T18:22:22+00:00Added an answer on May 14, 2026 at 6:22 pm

    So your structure is basically module/section/topic, and topics can appear in more than one section.

    Basically, you want this (content fields omitted):

    class Module(models.Model):
        title = CharField(max_length=100)
    
    class Section(models.Model):
        topics = ManyToManyField(Topic, related_name="sections", 
            through='TopicEntry')
        module = models.ForeignKey(Module, related_name="sections")
    
    class Topic(models.Model):
        text = models.TextField()
    
    class TopicEntry(models.Model):
        section = models.ForeignKey(Section)
        topic = models.ForeignKey(Topic)
        order = models.IntegerField()
    

    See Extra Fields on Many-to-Many Relationships for more information on ManyToManyField.through. The docs aren’t perfectly clear, though. If I read them right, to get your table of contents, use something like:

    topicList = Topic.objects.filter(section=section).order_by("TopicEntry__order")
    # Might be "topicEntry__order" or "topicentry__order" instead; the docs aren't 
    # clear how it transforms the name of the intermediate model.
    # This also works:
    topicList = section.topics.all().order_by("TopicEntry__order")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.