Here's what my model structure looks like:
class Visitor(models.Model):
id = models.AutoField(primary_key=True)
class Session(models.Model):
id = models.AutoField(primary_key=True)
visit = models.ForeignKey(Visitor)
sequence_no = models.IntegerField(null=False)
class Track(models.Model):
id = models.AutoField(primary_key=True)
session = models.ForeignKey(Session)
action = models.ForeignKey(Action)
when = models.DateTimeField(null=False, auto_now_add=True)
sequence_no = models.IntegerField(null = False)
class Action(models.Model):
id = models.AutoField(primary_key=True)
url = models.CharField(max_length=65535, null=False)
host = models.IntegerField(null=False)
As you can see, each Visitor
has multiple Sessions
; each Session
has multiple Tracks
and each Track
has one Action
. Tracks are always ordered ascendingly by the session
and the sequence_no
. A Visitors
average time on an site (i.e. a particular Action.host
) is the difference in Track.when
(time) between the highest and lowest Track.sequence_no
divided by the number of Sessions
of that Visitor
.
I need to calculate the average time of visitors on the site which be the sum of the time for each visitor on the Action.site
divided by the number of visitors.
I could query this using SQL but I'd like to keep my query as Djangonic as possible and I'm still very lost with complex queries.