i've got MenuItem model :
MenuItem(models.Model)
name=models.CharField(max_length=50)
url = models.URLField()
position = models.IntegerField()
Class Meta:
ordering =['position']
then i'm retriving开发者_JS百科 it by MenuItem.objects.all()
My question is how can i make any user friendly interface in admin panel to allow sorting MenuItems - for example list with + and - buttons to move MenuItem up and down ....
Start simple (for you as a developer), you can fairly easily display the position in a ListView.
You can add some javascript to turn each display of the position into a -/+ controller. You can make those +/- widgets have onclick handlers that do an ajax call to a custom view that handles the sorting.
The ajax view is fairly simple, you just grab the previous element in order:
old_pos = ItemToBumpDown.position
DisplacedItem = MenuItems.objects.filter(position__lt = ItemToBumpDown.position)[0]
ItemToBumpDown.position = DisplacedItem.position
DisplacedItem.position = old_pos
ItemToBumpDown.save()
DisplacedItem.save()
Your ajax callback when the call is complete can be to swap the admin row of the adjusted item with either it's previous or next element.
There's some polishing you might need to do, but this should be a start.
精彩评论