Package: django-lexorank
django-lexorankThis package implements an algorithm similar to JIRA's lexorank, but without using buckets for rebalancing
that can be used with Django projects.Installationpipinstalldjango-lexorankConfigurationAdddjango_lexorankto yourINSTALLED_APPSsetting:INSTALLED_APPS=[..."django_lexorank",]UsageDefining modelsTo add ranking to your model, you need to inherit it fromRankedModel.There are 2 ways of usingRankedModel:GloballyThis way, all the objects will be ranked together in the global scope.Example:fromdjango_lexorank.modelsimportRankedModelfromdjango.dbimportmodelsclassBoard(RankedModel):name=models.CharField(max_length=255)Per groupThis way, instances of the model will be ranked separately per group.To do that, you have to set a name of the foreign key to the group
to theorder_with_respect_toparameter of the model.Example:fromdjango_lexorank.modelsimportRankedModelfromdjango.dbimportmodelsclassTask(RankedModel):name=models.CharField(max_length=255)board=models.ForeignKey("Board",on_delete=models.CASCADE,related_name="tasks")order_with_respect_to="board"Field parametersBy default, new instances of the model will be ranked at the top of the list.
Rank field may accept boolean parameterinsert_to_bottomto override this behaviour.Example:fromdjango_lexorank.modelsimportRankedModelfromdjango_lexorank.fieldsimportRankFieldfromdjango.dbimportmodelsclassUser(RankedModel):name=models.CharField(max_length=255)rank=RankField(insert_to_bottom=True)Manager methodsThere are 3 ways to insert models using manager methods:model.objects.create(**kwargs)- will use the default behaviour specified onRankFielddefinition.model.objects.add_to_bottom(**kwargs)- will insert the model at the bottom of the list.model.objects.add_to_top(**kwargs)- will insert the model at the top of the list.Instance methodsobj.place_after(after_obj)- places model instance after provided instance.
If rank length exceeds the limit after the move, rebalancing will be scheduled.obj.place_before(before_obj)- places model instance before provided instance.
If rank length exceeds the limit after the move, rebalancing will be scheduled.obj.place_on_top()- moves model instance to the bottom of the list.
If rank length exceeds the limit after the move, rebalancing will be scheduled.obj.place_on_bottom()- moves model instance to the bottom of the list.
If rank length exceeds the limit after the move, rebalancing will be scheduled.obj.get_previous_object()- return previous object in the listobj.get_next_object()- return next object in the listobj.get_previous_object_rank()- return previous object rank in the listobj.get_next_object_rank()- return next object rank in the listobj.schedule_rebalancing()- schedule rebalancing  for the whole list or a group iforder_with_respect_tois setobj.rebalance()- rebalance the whole list or a group iforder_with_respect_tois setobj.rebalancing_required()- returnsTrueif rebalancing is required for the whole list,
or for a group iforder_with_respect_tois setobj.rebalancing_scheduled()- returnsTrueif rebalancing is scheduled for the whole list,
or for a group iforder_with_respect_tois setModel methodsmodel.get_first_object()- return first object in the listmodel.get_last_object()- return last object in the listmodel.get_first_object_rank()- return first object rank in the listmodel.get_last_object_rank()- return last object rank in the listRebalancing ScheduleEach time, a rank length exceeds the limit, rebalancing is scheduled for the whole list or a group,
according to the value oforder_with_respect_toparameter.SheduledRebalancingmodel can be used to create a task for rebalancing ranks.