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).