Welcome to Django Reorder’s documentation!¶
Contents:
Django Reorder¶

A project that helps sorting querysets in a specific order
Documentation¶
The full documentation is at https://django-reorder.readthedocs.org.
Quickstart¶
Install Django Reorder:
pip install django-reorder
Then use it in a project:
from django_reorder.reorder import reorder
Tshirt.objects.order_by(reorder(size=['S', 'M', 'L']))
Some more detailed examples can be foind on the Usage page.
Features¶
- Can be used in
order_by()
and inannotate()
calls. - Works across relationships.
- Lets you control the sorting order of
NULL
values explicitly (otherwise it can vary across databases).
Running Tests¶
Does the code actually work?
source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_test.txt
(myenv) $ python runtests.py
Usage¶
To use Django Reorder in a project:
from django_reorder.reorder import reorder
Consider the following model:
SIZES = [
('XXS', 'XX-Small'),
('XS', 'X-Small'),
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
('XL', 'X-Large'),
('XXL', 'XX-Large'),
]
class Tshirt(models.Model):
name = models.CharField(max_length=100)
size = models.CharField(max_length=5, choices=SIZES)
def __str__(self):
return self.name
If you try to do something like Tshirt.objects.order_by('size')
, then you
get alphabetical sorting on the size (L, M, S, XL, XS, XXL, XXS) which is
probably not what you want.
With django-reorder, you can do:
Tshirt.objects.order_by(reorder(size=['XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL']))
And your tshirts will be sorted by their size.
Reference¶
-
django_reorder.reorder.
reorder
(<fieldname>=<new order>, _default=AFTER, _reverse=False)[source]¶ This function generates a query expression (suitable for passing directly in
order_by
orannotate
) that will sort the queryset by the given<fieldname>
so that rows appear in the order given by<new_order>
.It takes two optional parameters (their names are prefixed by an underscore to prevent clashing with potential field names):
_default
controls whether values that don’t appear in<new order>
are sorted before or after (the default) those that do. Use theBEFORE
andAFTER
constants that can be imported from the module._reverse
lets you reverse the sorting.
-
django_reorder.reorder.
null_first
(fieldname)[source]¶ This function is a small wrapper around
reorder()
and lets you control the sorting order ofNULL
values for a given field (otherwise your database will decide whether to putNULL
values at the beginning or at the end, and different databases do it differently).It takes a single required argument: the name of the field (as a string) whose
NULL
values should be sorted first.- class Tshirt(models.Model):
- ... purchased_on = models.DateTimeField(blank=True, null=True)
# This will yield all Tshirt objects and those without a purchase date # will be listed first: Tshirt.objects.order_by(null_first(‘purchased_on’))
# You can also combine null_first() with other sorting fields. # For example this will sort Tshirt by their purchase date but it will list # T-shirts without a purchase date first: Tshirt.objects.order_by(null_first(‘purchased_on’), ‘purchased_on’)
Contributing¶
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
You can contribute in many ways:
Types of Contributions¶
Report Bugs¶
Report bugs at https://github.com/bmispelon/django-reorder/issues.
If you are reporting a bug, please include:
- Your operating system name and version.
- Any details about your local setup that might be helpful in troubleshooting.
- Detailed steps to reproduce the bug.
Fix Bugs¶
Look through the GitHub issues for bugs. Anything tagged with “bug” is open to whoever wants to implement it.
Implement Features¶
Look through the GitHub issues for features. Anything tagged with “feature” is open to whoever wants to implement it.
Write Documentation¶
Django Reorder could always use more documentation, whether as part of the official Django Reorder docs, in docstrings, or even on the web in blog posts, articles, and such.
Submit Feedback¶
The best way to send feedback is to file an issue at https://github.com/bmispelon/django-reorder/issues.
If you are proposing a feature:
- Explain in detail how it would work.
- Keep the scope as narrow as possible, to make it easier to implement.
- Remember that this is a volunteer-driven project, and that contributions are welcome :)
Get Started!¶
Ready to contribute? Here’s how to set up django-reorder for local development.
Fork the django-reorder repo on GitHub.
Clone your fork locally:
$ git clone git@github.com:your_name_here/django-reorder.git
Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:
$ mkvirtualenv django-reorder $ cd django-reorder/ $ python setup.py develop
Create a branch for local development:
$ git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:
$ flake8 django_reorder tests $ python setup.py test $ tox
To get flake8 and tox, just pip install them into your virtualenv.
Commit your changes and push your branch to GitHub:
$ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature
Submit a pull request through the GitHub website.
Pull Request Guidelines¶
Before you submit a pull request, check that it meets these guidelines:
- The pull request should include tests.
- If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
- The pull request should work for Python 2.6, 2.7, and 3.3, and for PyPy. Check https://travis-ci.org/bmispelon/django-reorder/pull_requests and make sure that the tests pass for all supported Python versions.
Credits¶
Development Lead¶
- Baptiste Mispelon <bmispelon@gmail.com>
Contributors¶
None yet. Why not be the first?