models with order (+admin editing)
Use this abstract model if you want to add "order" to a given model. Once you do, you will get automatic "up" and "down" links for each model row.
One problem is that if the user sorts by another row, the up and down links are still there, but now don't make a lot of sense.
Thanks for this! I went looking for how to order user data in ModelAdmin and was led here. I agree w/andybak, this would be nice to be ajax-y. It would be really nice w/some up/down triangles too.
It looks like the metaclass needs to stay w/the models, unless you splay it out to another file, in which case you would need to also import it. Yes, the view is only for the admin. good luck w/your django site, mdgart!
Thanks for the snippet! Found a bug in move_up() though...
With the code as is, whenever you try to move something up, it goes to the top of the list. This is because you're querying the database for the first element in the collection of elements whose order is less than the current element. Since we aren't ordering, the first element will always have order 0.
Change the lower_model assignment to this:
lower_model = ModelClass.objects.filter(order__lt=higher_model.order).order_by('-order')
(note the order_by) and it should work properly.