Django Nested Query
Project description
django-nestedquery
Given a django model such as
class SensorReading(models.Model):
sensor=models.PositiveIntegerField()
timestamp=models.DatetimeField()
reading=models.IntegerField()
This allows the construction of queries such as
from nestedquery import NestedQuery
qs=NestedQuery(SensorReading.objects.filter(sensor=1)).filter(reading__gte=10)
resulting in SQL looking something like
SELECT * FROM (
SELECT * FROM SensorReading
WHERE sensor = 1
) as VirtualTable
WHERE VirtualTable.reading >= 10;
While this example is obviously simplistic, This Pattern becomes more useful when dealing with more complex queries, for example when dealing with aggregates
readings = (
NestedQuery(
SensorReading.objects.filter(**filters)
.annotate(
previous_read=Window(
expression=window.Lead("timestamp"),
partition_by=[F("sensor"),],
order_by=[
"timestamp",
],
frame=RowRange(start=-1, end=0),
)
)
.annotate(delta=Abs(Extract(F("timestamp") - F("previous_read"), "epoch")))
)
.values("sensor")
.annotate(min=Min("delta"), max=Max("delta"))
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_nestedquery-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67a0de064b5bb2bdf48ee6c0fa607435d3afeebe45e367dfc71faba086938758 |
|
MD5 | 6b0f0b0221b20adfad83cd300f6aa9f7 |
|
BLAKE2b-256 | 4273ba2bcd32eefb6fc437e16a04963c8eb174df8cf2ad9bc8f24fbe2d7cab72 |