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.
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.
In model class you would probably have to add "order" field, to maintain specific order (eg. item with order = 10 is the last one and order = 1 is the first one). Then you can add a JS code in admin change_list template (see this) to maintain drag&drop feature. Finally change ordering in Meta of model to something like ['order'].
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.
For working code to do this, check out snippet 1053 at djangosnippets.org.