RankedModel

class awl.rankedmodel.models.RankedModel(*args, **kwargs)

Abstract model used to have all the inheritors ordered in the database by this model’s rank field. Ranks can either be across all instances of the inheriting model, or in a series of groups. See RankedModel.group_filter for details on implementing grouping.

The rank field can be set and saved like any other field. The overridden RankedModel.save method maintains rank integrity. The order is maintained but it is not guaranteed that there are not gaps in the rank count – for simplicity re-ordering is not done on deletion. If empty slots are a concern, use RankedModel.repack.

Warning

Due to the use of the overridden save() caution must be employed when dealing with any update() calls or raw SQL as these will not call the save() method.

Two admin helper functions are provided so you can do rank re-ordering in the django admin. To use the functions, add columns to your admin instances of the inheriting class:

from awl.rankedmodel.admintools import (admin_link_move_up, 
    admin_link_move_down)

@admin.register(Favourites)
class FavouritesAdmin(admin.ModelAdmin):
    list_display = ('name', 'rank', 'move_up', 'move_down')

    def move_up(self, obj):
        return admin_link_move_up(obj)
    move_up.allow_tags = True
    move_up.short_description = 'Move Up Rank'

    def move_down(self, obj):
        return admin_link_move_down(obj)
    move_down.allow_tags = True
    move_down.short_description = 'Move Down Rank'
Parameters:

rank – Ranked order of object

__init__(*args, **kwargs)
grouped_filter()

This method should be overridden in order to allow groupings of RankModel objects. The default is there is a single group which are all instances of the inheriting class.

An example with a grouped model would be:

class Grouped(RankedModel):
    group_number = models.IntegerField()

    def grouped_filter(self):
        return Grouped.objects.filter(
            group_number=self.group_number)
Returns:

QuerySet of RankedModel objects that are in the same group.

repack()

Removes any blank ranks in the order.

save(*args, **kwargs)

Overridden method that handles that re-ranking of objects and the integrity of the rank field.

Parameters:

rerank – Added parameter, if True will rerank other objects based on the change in this save. Defaults to True.

RankedModel Admin

Two admin helper functions are provided so you can do rank re-ording in the django admin. To use the helper functions you will need to have awl.rankedmodel in your INSTALLED_APPS and include the urls.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'awl.rankedmodel',
)
urlpatterns = [
    url('admin/', include(admin.site.urls)),
    url('rankedmodel/', include('awl.rankedmodel.urls')),
]

The provided functions act return links to a django admin view that can move the associated objects. To use them, add them as columns in the admin model of your inheriting class.

from awl.rankedmodel.admintools import admin_link_move_up, admin_link_move_down

@admin.register(Favourites)
class FavouritesAdmin(admin.ModelAdmin):
    list_display = ('name', 'rank', 'move_up', 'move_down')

    def move_up(self, obj):
        return admin_link_move_up(obj)
    move_up.short_description = 'Move Up Rank'

    def move_down(self, obj):
        return admin_link_move_down(obj)
    move_down.short_description = 'Move Up Rank'

Returns a link to a view that moves the passed in object down in rank.

Parameters:
  • obj – Object to move

  • link_text – Text to display in the link. Defaults to “↓”

Returns:

HTML link code to view for moving the object

Returns a link to a view that moves the passed in object up in rank.

Parameters:
  • obj – Object to move

  • link_text – Text to display in the link. Defaults to “↑”

Returns:

HTML link code to view for moving the object

Returns a formatted column with up to two links in it for moving the passed in object up or down in rank.

Parameters:
  • obj – Object to move

  • up_text – Text to display in the up link. Defaults to “↑”

  • down_text – Text to display in down the link. Defaults to “↓”

Returns:

HTML link code to view for moving the object