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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:42:44+00:00 2026-05-13T08:42:44+00:00

Ok, imagine a typical scenario, – there is a thread object and comments, attached

  • 0

Ok, imagine a typical scenario, – there is a thread object and comments, attached to it. It can be expressed in django ORM’s terms very roughly like this:

class Thread(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)

class Comment(models.Model):
    id = models.AutoField(primary_key=True)
    content = models.TextField()
    created = models.DateTimeField(editable=False)
    thread = models.ForeignKey(Thread, related_name='comments')

    def save(self, force_insert=False, force_update=False):
        self.created = datetime.datetime.now()
        super(Comment, self).save(force_insert, force_delete)

So, when displaying the actual data, the comments are going to be ordered by their creation time (since I like not to make an assumption, that an identity field also guarantees you the correct order of the records).

But what if I needed to be able to control (and alter) the order in which the comments must appear within a thread and to be able to move comments from one thread to another.

This could be accomplished by adding, for example, a “sequence” field to the comment model and restricting the uniqueness of the combination of the “sequence” field and “thread” field of the comment model (so that there can be no comments within a thread with the same sequence number). A unique key on these fields should be sufficient (in terms of django, it can be accomplished via unique_together, I think).

However, this adds a computational (and coding :P) overhead, because if you are to change the order of the comments, by changing their sequence numbers, you must also update the comments with sequence numbers, higher, than the target comment’s sequence number. And so on. It is just asking for trouble.

Somehow, I just can’t get my head around this. Maybe I am just missing something?

(If it is possible, don’t regard this as a django-only problem. IMO it is language-agnostic-ish, since it has nothing django-specific about it, when you think of it)

EDIT: I’ve suddenly realized, that there is no actual question in the, err, question. So the question is, what would be the most lightweight way to implement a such a model, besides the obvious way I’ve described (seems too cumbersome to me).

  • 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-13T08:42:45+00:00Added an answer on May 13, 2026 at 8:42 am

    If I understand your question (not entirely sure I do), you want to create and maintain an arbitrary ordering of Comments relative to a Thread.

    The only ways I know of doing this is to either have an ordering field in the Comment, or a list-of-Comment-ids-in-order field in the Thread, or possibly a separate table that has a (Thread-id, Comment-id, order) tuple in it.

    For a website I did about 2 years ago we went with the middle option for a Newsletter which contained 10-30 articles. I used some jQuery magic on the Newsletter’s admin page and it let the admins shuffle the Article titles around while in the background it kept updating the Article-ids-in-order field of the Newsletter.

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

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The finderSortWithLocale method (both these are taken from apple api):… May 15, 2026 at 5:00 pm
  • Editorial Team
    Editorial Team added an answer I could bang my head. The solution was simple: Alias… May 15, 2026 at 5:00 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure what the name would be of such… May 15, 2026 at 5:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.