Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 30, 2010 19:56:58

f.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding extra data in annotate()


All,

I'm attempting to consolidate many orm queries into one. My models:

class Key (models.Model):
name = models.CharField()

class Value (models.Model):
data = models.FloatField()
timestamp = models.DateTimeField()
key = models.ForeignKey(Key)

Given a list of Key names, I am able to annotate the count of Value
objects associated. However, I would also like to construct a query
that will take a list of Key names and find an aggregate (Min, Max)
Value with additional annotations (timestamp).

Currently, it's an inefficient loop:

>>> # Simulate input parameters.
>>> key_names =
>>> end = datetime.datetime.now()
>>> start = end - datetime.timedelta(days=-1)
>>>
>>> # output dict.
>>> lasts = {}
>>>
>>> for key_name in key_names:
... # Narrow down the Value objects by key name.
... qs = Value._default_manager.filter(key__name=key_name)
... # And by date range.
... qs = qs.filter(value__timestamp__range=(start, end))
... # Find the most recent timestamp.
... last = qs.aggregate(last=Max("timestamp"))
... # With the most recent timestamp, filter the queryset for
the matching record.
... value = qs.filter(timestamp=last)
... # Append to output
... lasts = {"timestamp" : value.timestamp, "data" :
value.data}
>>> lasts

Is there a more efficient way to aggregate and/or annotate all of the
latest Value data and timestamp object fields onto the associated Key
object?

TIA

Franco

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Nov. 30, 2010 21:01:30

f.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding extra data in annotate()


Ideally any proposed solution could also substitute the search for the
newest timestamp via Max() to be the largest Value.data float with
similar logic.

Franco

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 1, 2010 04:22:24

f.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding extra data in annotate()


For reference, it looks like D.Rosman has already wrote about the same
topic athttp://blog.roseman.org.uk/.

Franco

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support