Skip to main content

Some utility functions to perform complex queries with the Django ORM

Project description

dj-querytools

A collection of functions to make it a little easier to build aggregation SQL queries for Django projects

Installation

pip install dj-querytools

Usage

Simple aggregation:

Assuming the following model:

class Todo(models.Model):
    owner = models.CharField(max_length=30)
    title = models.CharField(max_length=30)
    status = models.CharField(max_length=30, choices=TODO_STATUSES)
    size = models.PositiveIntegerField(default=1)
    due = models.DateField()
    due_time = models.DateTimeField()

group_by_and_aggregate

Example

group_by_and_aggregate(
    qs,
    'size',
    'Sum'
)
>> 20

group_by_and_annotate

Performs the aggregation and groups by the provided field:

Example

result = group_by_and_annotate(
    qs,
    'status',
    'Count'
)
>> {'D': 5, 'N': 1, 'P': 8}

as_timeseries

qs = Todo.objects.filter(owner="Jack")
as_timeseries(
    qs,
    'due_time',
    'id',
    'Count',
    '2018-11-01',
    '2018-11-10'
)
>> [{'x': '2018-11-01', 'y': 3.0}, {'x': '2018-11-02', 'y': 3.0}, {'x': '2018-11-03', 'y': 0}, {'x': '2018-11-04', 'y': 2.0}, {'x': '2018-11-05', 'y': 2.0}, {'x': '2018-11-06', 'y': 0}, {'x': '2018-11-07', 'y': 0}, {'x': '2018-11-08', 'y': 0}, {'x': '2018-11-09', 'y': 0}, {'x': '2018-11-10', 'y': 0}]

pivot_table

Given a flat queryset, group it by one or more fields.

  • fields are comma seperated
  • optionally provide a serializer function to serialize items (otherwise it will just append the queryset row)
result = pivot_table(qs, 'owner,status', serialize_todo)

>> {'Jack': {'N': [{'title': 'Day year outside whatever int'}], 'P': [{'title': 'Answer society close example '}, {'title': 'Raise real individual general'}], 'D': [{'title': 'Number organization particula'}, {'title': 'Least activity herself than c'}, {'title': 'Country local pretty yourself'}]}, 'Jane': {'P': [{'title': 'Prevent do still teacher grou'}, {'title': 'Include they management first'}, {'title': 'Number court few loss sort mu'}, {'title': 'That federal end local dream '}, {'title': 'Able ask listen. Cost ball me'}, {'title': 'Bad know meeting compare room'}], 'D': [{'title': 'Through town stay able car di'}, {'title': 'Tree design candidate because'}]}}

periodic_breakdown

from querytools.tools import periodic_breakdown

Provide an aggregation broken down by a period (Trun(kind=)).

See: https://docs.djangoproject.com/en/2.1/ref/models/database-functions/#django.db.models.functions.TruncMonth

Defaults:

  • kind='month'
  • aggregate_field='ok'
  • aggregation = 'Count'
# simple case:
result = periodic_breakdown(qs, 'due')

# slighly more fancy:
result = periodic_breakdown(
    qs,
    'due',
    aggregate_field='size',
    aggregation = 'Sum'
)

Other stuff

  • For full functionality list, see spec.txt
  • For examples, check example_app/tests.py

Contributing

Update the spec:

python manage.py test --testrunner=testreporter.runner.BDDTestRunner

Project details


Download files

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

Source Distribution

dj-querytools-0.2.3.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file dj-querytools-0.2.3.tar.gz.

File metadata

  • Download URL: dj-querytools-0.2.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using 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.1 CPython/3.7.6

File hashes

Hashes for dj-querytools-0.2.3.tar.gz
Algorithm Hash digest
SHA256 7d944349aca6b1ca1bb0185a2322411ef27af6232409e6de769d1f234a74209e
MD5 74488fca7fff7d953f33c90c63f3a9ec
BLAKE2b-256 313618905717511d4329f7d8d6a14a2431457e1c96c0773de8c435913fc635a9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page