Skip to main content

Circle CI codecov Latest Version

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:

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

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')

Metadata

Release files for django-next-prev 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-next-prev 1.1.0
File Size Uploaded Provenance
django-next-prev-1.1.0.tar.gz 4.8 kB Without provenance Details

Release files / django-next-prev-1.1.0.tar.gz

Download URL django-next-prev-1.1.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
eb79dcde18bb0c75c809224ed8656417fb3e56eb7e7064552772ffb11d203885
BLAKE2b-256 checksum
How to use checksums
d49b00bd08cdd54e8e9cf3a321b59d8ad8bf287b7373ec06de7048696cc267b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.1

Security for django-next-prev 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page