Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Extended QuerySets for Django

Project description


ExtendedQuerySets is meant to simplify the way to retrieve one object from db using Django QuerySets.


$ pip install extquerysets


For the following examples assume that we have a model Player, and we've implemented custom Manager and QuerySet for that model.

Task 1: Retrieve one object or return None if object doesn't exist.

Usual way:

   winner = Player.objects.winner()
except Player.DoesNotExist:
   winner = None

Using ExtendedQuerySets:

winner = Player.objects.winner_or_none()

Task 2: Retrieve one object or raise Http404 if object doesn't exist.

Usual way:

   winner = Player.objects.winner()
except Player.DoesNotExist:
   raise Http404

Using ExtendedQuerySets:

winner = Player.objects.winner_or_404()


from django.db import models

from .querysets import PlayerQuerySet

class Player(model.Model):
    name = models.CharField(_('Name'), max_length=12)
    is_winner = models.BooleanField(_('Is winner'), default=False)
    objects = PlayerQuerySet.as_manager()

from extquerysets.base import CustomQuerySet
from extquerysets.decorators import or_404
from extquerysets.decorators import or_none

class PlayerQuerySet(CustomQuerySet):
    def get(self, *args, **kwargs):
        return super().get(*args, **kwargs)

    def winner(self):
        return self.get(is_winner=True)

Now you can retrieve Player instance:

from .models import Player


player = Player.objects.winner_or_none()
player = Player.objects.winner_or_404()
player = Player.objects.filter(name__icontains='Tom').winner_or_none()
player = Player.objects.filter(name__icontains='Tom').winner_or_404()
player = Player.objects.get_or_none(pk=10)
player = Player.objects.get_or_404(pk=10)


More info about custom Managers and QuerySets:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for extquerysets, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size extquerysets-0.0.4.tar.gz (3.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page