Skip to main content

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

Release files for dj-querytools 0.2.3

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

Source distribution (sdist)

Source distribution for dj-querytools 0.2.3
File Size Uploaded
dj-querytools-0.2.3.tar.gz 4.2 kB Details

Release files / dj-querytools-0.2.3.tar.gz

Download URL dj-querytools-0.2.3.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7d944349aca6b1ca1bb0185a2322411ef27af6232409e6de769d1f234a74209e
BLAKE2b-256 checksum
How to use checksums
313618905717511d4329f7d8d6a14a2431457e1c96c0773de8c435913fc635a9
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.1 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.2.3 This release

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.0

1 release file

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