`Circle CI <https://circleci.com/gh/gregplaysguitar/django-next-prev>`__
`codecov <https://codecov.io/gh/gregplaysguitar/django-next-prev>`__
`Latest Version <https://pypi.python.org/pypi/django-next-prev/>`__
Purpose
-------
django-next-prev provides utilities to get the next or previous item
within an ordered, filtered queryset. For example, you could display a
link to the next featured post on a post detail page, or show
chronological next and previous links for all published posts.
Installation
------------
Download the source from https://pypi.python.org/pypi/django-next-prev/
and run ``python setup.py install``, or:
::
> pip install django-next-prev
Django 1.8 or higher is required.
Quick start
-----------
Given this models.py:
.. code:: python
from django.db import models
class Category(models.Model):
title = models.CharField(max_length=100)
class Post(models.Model):
title = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
created = models.DateField()
featured = models.BooleanField(default=False)
text = models.TextField()
class Meta:
# order on primary key to make sure it's unique
ordering = ('created', 'title', 'pk')
You can do the following
.. code:: python
from next_prev import next_in_order, prev_in_order
from .models import Post
# default ordering
first = Post.objects.first()
second = next_in_order(first)
prev_in_order(second) == first # True
last = prev_in_order(first, loop=True)
# custom ordering
qs = Post.objects.all().order_by('-created', '-pk')
newest = qs.first()
second_newest = next_in_order(newest, qs=qs)
oldest = prev_in_order(newest, qs=qs, loop=True)
# filtered
qs = Post.objects.filter(featured=True)
first_featured = qs.first()
second_featured = next_in_order(first_featured, qs=qs)
Ordering considerations
~~~~~~~~~~~~~~~~~~~~~~~
Order fields must be non-nullable.
If results are inconsistent with the order of your queryset, you’re
probably ordering on a non-unique combination of fields. For example,
``ordering = ('sort_order', )`` with the duplicate ``sort_order``
values. To resolve this, add a unique field to your ordering, i.e.
``ordering = ('sort_order', 'pk')``
Raw data
{
"_id": null,
"home_page": "https://github.com/gregplaysguitar/django-next-prev",
"name": "django-next-prev",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Greg Brown",
"author_email": "greg@gregbrown.co.nz",
"download_url": "https://files.pythonhosted.org/packages/d4/9b/00bd08cdd54e8e9cf3a321b59d8ad8bf287b7373ec06de7048696cc267b1/django-next-prev-1.1.0.tar.gz",
"platform": "any",
"description": "`Circle CI <https://circleci.com/gh/gregplaysguitar/django-next-prev>`__\n`codecov <https://codecov.io/gh/gregplaysguitar/django-next-prev>`__\n`Latest Version <https://pypi.python.org/pypi/django-next-prev/>`__\n\nPurpose\n-------\n\ndjango-next-prev provides utilities to get the next or previous item\nwithin an ordered, filtered queryset. For example, you could display a\nlink to the next featured post on a post detail page, or show\nchronological next and previous links for all published posts.\n\nInstallation\n------------\n\nDownload the source from https://pypi.python.org/pypi/django-next-prev/\nand run ``python setup.py install``, or:\n\n::\n\n > pip install django-next-prev\n\nDjango 1.8 or higher is required.\n\nQuick start\n-----------\n\nGiven this models.py:\n\n.. code:: python\n\n from django.db import models\n\n class Category(models.Model):\n title = models.CharField(max_length=100)\n\n class Post(models.Model):\n title = models.CharField(max_length=100)\n category = models.ForeignKey(Category, on_delete=models.CASCADE)\n created = models.DateField()\n featured = models.BooleanField(default=False)\n text = models.TextField()\n\n class Meta:\n # order on primary key to make sure it's unique\n ordering = ('created', 'title', 'pk')\n\nYou can do the following\n\n.. code:: python\n\n from next_prev import next_in_order, prev_in_order\n from .models import Post\n\n # default ordering\n first = Post.objects.first()\n second = next_in_order(first)\n prev_in_order(second) == first # True\n last = prev_in_order(first, loop=True)\n\n # custom ordering\n qs = Post.objects.all().order_by('-created', '-pk')\n newest = qs.first()\n second_newest = next_in_order(newest, qs=qs)\n oldest = prev_in_order(newest, qs=qs, loop=True)\n\n # filtered\n qs = Post.objects.filter(featured=True)\n first_featured = qs.first()\n second_featured = next_in_order(first_featured, qs=qs)\n\nOrdering considerations\n~~~~~~~~~~~~~~~~~~~~~~~\n\nOrder fields must be non-nullable.\n\nIf results are inconsistent with the order of your queryset, you\u2019re\nprobably ordering on a non-unique combination of fields. For example,\n``ordering = ('sort_order', )`` with the duplicate ``sort_order``\nvalues. To resolve this, add a unique field to your ordering, i.e.\n``ordering = ('sort_order', 'pk')``",
"bugtrack_url": null,
"license": "BSD License",
"summary": "Django utility to retrieve the next or previous object, given a current object and a queryset.",
"version": "1.1.0",
"project_urls": {
"Homepage": "https://github.com/gregplaysguitar/django-next-prev"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d49b00bd08cdd54e8e9cf3a321b59d8ad8bf287b7373ec06de7048696cc267b1",
"md5": "83f74d61d4417493ef55bec62ea48a0b",
"sha256": "eb79dcde18bb0c75c809224ed8656417fb3e56eb7e7064552772ffb11d203885"
},
"downloads": -1,
"filename": "django-next-prev-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "83f74d61d4417493ef55bec62ea48a0b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4841,
"upload_time": "2021-01-07T20:39:06",
"upload_time_iso_8601": "2021-01-07T20:39:06.158121Z",
"url": "https://files.pythonhosted.org/packages/d4/9b/00bd08cdd54e8e9cf3a321b59d8ad8bf287b7373ec06de7048696cc267b1/django-next-prev-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-01-07 20:39:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gregplaysguitar",
"github_project": "django-next-prev",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"circle": true,
"tox": true,
"lcname": "django-next-prev"
}