python - Django's ORM. Group by elements with difference -
i have django model:
class visits(models.model): ip = models.genericipaddressfield() url = models.charfield(max_length=200) datetime = models.datetimefield(auto_now_add=true) def __unicode__(self): return "%s : %s (%s)" % (unicode(self.ip), unicode(self.datetime.strftime('%d/%m/%y %h:%m:%s')), unicode(self.url))
it contains visits website. example, these values db:
<visits: 127.0.0.1 : 27/11/14 00:00:00 (/)>, <visits: 127.0.0.1 : 27/11/14 00:01:00 (/)>, <visits: 127.0.0.1 : 27/11/14 00:09:00 (/)>, <visits: 127.0.0.1 : 27/11/14 00:45:00 (/)>, <visits: 127.0.0.1 : 27/11/14 00:46:00 (/)>
i want write orm query groups values 15-minutes delta. example, query should return count()==2
(first 3 values in first group, second 2 in second). how can using django's orm?
here have 4 visits , tried filter out minitues grater 28.
>>> visits_count = len([ obj obj in visits.objects.all() if obj.datetime.strftime('%m') > '28']) >>> visits_count 2
it case minitues greater equal 28.
>>> visits_count = len([ obj obj in visits.objects.all() if obj.datetime.strftime('%m') >= '28']) >>> visits_count 4
Comments
Post a Comment