Django menu item sorting

I have a MenuItem model:

MenuItem(models.Model)
name=models.CharField(max_length=50)
url = models.URLField() 
position = models.IntegerField() 

Class Meta: 
ordering =['position'] 

      

then i return it using MenuItem.objects.all ()

My question is, how can I make any user-friendly interface in the admin panel to allow MenuItems to be sorted - for example, a list with the + and - buttons to move the MenuItem up and down ....

+2


source to share


1 answer


To start simple (for you as a developer), you can display a position in a ListView quite easily.

You can add some javascript to turn each position display into a - / + controller. You can make these +/- widgets with onclick handlers that make an ajax call to a custom view that handles sorting.

The Ajax view is pretty simple, you just take 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()

      

The ajax callback after the call completes could be to swap the administrative line of the configured element to the previous or next element.

You may need polishing there, but that should be the start.

0


source







All Articles