[Answered ]-How do swap the indexes of a Posts?

1👍

def swap_up(item):
    nextItem = Posts.objects.filter(order_index__gt=item.order_index).order_by('order_index').first()
    if nextItem:
        item_index = item.order_index
        item.order_index = nextItem.order_index
        nextItem.order_index = item_index

        item.save()
        nextItem.save()

def swap_down(item):
    prevItem = Posts.objects.filter(order_index__lt=item.order_index).order_by('order_index').last()
    if prevItem:
        item_index = item.order_index
        item.order_index = prevItem.order_index
        prevItem.order_index = item_index

        item.save()
        prevItem.save()

Leave a comment