Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-ordering entries in a model using drag-and-drop

Say I have a blogging app in Django. How can i re-order the posts using a draggable table in the default admin?

It would be best if i didn't have to add any extra fields to the model, but if i really have to i can.

like image 927
Josh Hunt Avatar asked Sep 21 '08 11:09

Josh Hunt


2 Answers

For working code to do this, check out snippet 1053 at djangosnippets.org.

like image 153
Carl Meyer Avatar answered Sep 30 '22 09:09

Carl Meyer


Note on the "It would be best if i didn't have to add any extra fields to the model, but if i really have to i can."

Sorry, but order of information in a database is determined by the information itself: you always have to add a column for ordering. There's really no choice about that.

Further, to retrieve things in this order, you'll need to specifically add .order_by(x) to your queries or add ordering to your model.

class InOrder( models.Model ):
    position = models.IntegerField()
    data = models.TextField()
    class Meta:
        ordering = [ 'position' ]

Without the additional field ordering cannot happen. It's one of the rules of relational databases.

like image 26
S.Lott Avatar answered Sep 30 '22 09:09

S.Lott