I have a django project which stores real-time data of a few devices. For this manner I have used timescaledb which is suitable for time series. Timescale offers a few hyperfunctions which I need to use (specifically lttb
which is used for downsampling data).
For example, this is one of the queries that I am looking to achieve:
SELECT time as timestamp, value as value, %s as device_id
FROM unnest((SELECT lttb(timestamp, value, %s)
FROM core_devicedata where device_id=%s and timestamp between %s and %s))
I can get result of this query as a raw query set by:
for data in DeviceData.objects.raw(query):
...
I have already tried raw sql queries using django. They work. The thing is they offer no filtering and ordering functionality since they do not return actual queryset. Instead, they return a raw query set. What I am trying to achieve is to run a query like below using only with power of djagno orm.
SELECT time as timestamp, value as value, %s as device_id
FROM unnest((SELECT lttb(timestamp, value, %s)
Any suggestions? If there is no way to do this with django orm itself, would please help me write a modular manager method for this manner? Like I should be able to filter or order only if I want.
thisisjab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.