managers.py 1.2 KB

123456789101112131415161718192021222324252627282930
  1. from django.db.models import Manager
  2. class NaturalOrderByManager(Manager):
  3. def natural_order_by(self, *fields):
  4. """
  5. Attempt to order records naturally by segmenting a field into three parts:
  6. 1. Leading integer (if any)
  7. 2. Middle portion
  8. 3. Trailing integer (if any)
  9. :param fields: The fields on which to order the queryset. The last field in the list will be ordered naturally.
  10. """
  11. db_table = self.model._meta.db_table
  12. primary_field = fields[-1]
  13. id1 = '_{}_{}1'.format(db_table, primary_field)
  14. id2 = '_{}_{}2'.format(db_table, primary_field)
  15. id3 = '_{}_{}3'.format(db_table, primary_field)
  16. queryset = super(NaturalOrderByManager, self).get_queryset().extra(select={
  17. id1: "CAST(SUBSTRING({}.{} FROM '^(\d{{1,9}})') AS integer)".format(db_table, primary_field),
  18. id2: "SUBSTRING({}.{} FROM '^\d*(.*?)\d*$')".format(db_table, primary_field),
  19. id3: "CAST(SUBSTRING({}.{} FROM '(\d{{1,9}})$') AS integer)".format(db_table, primary_field),
  20. })
  21. ordering = fields[0:-1] + (id1, id2, id3)
  22. return queryset.order_by(*ordering)