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.
For working code to do this, check out snippet 1053 at djangosnippets.org.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With