从 Django 查询数据
我的模型结构如下:
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)
如您所见,每个 Visitor
都有多个 Session
;每个Session
有多个Track
,每个Track
有一个Action
。曲目始终按session
和sequence_no
升序排序。 Visitors
在网站(即特定 Action.host
)上的平均时间是最高和最高值之间 Track.when
(时间)的差值。最低的 Track.sequence_no
除以该访问者
的会话
数量。
我需要计算访问者在网站上的平均时间,即每个访问者在 Action.site
上的时间总和除以访问者数量。
我可以使用 SQL 进行查询,但我希望尽可能保持 Djangonic 的查询,但我仍然对复杂的查询感到非常困惑。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于特定的
Action
对象,您可以收集有关会话的有趣数据:您将得到以下内容:
现在只需从数据中获取所需结果即可:
另一种方法是使用两个查询而不是一个,并避免使用
len(set(...))
片段:除了提供的聚合之外,没有办法执行实际的计算字段,因此要么使用原始 SQL 执行,要么执行在这样的代码中。
至少建议的解决方案尽可能使用Django的ORM。
For a specific
Action
object you can gather interesting data about Sessions:You will get something in the line of:
Now it's only a matter of getting the desired result from the data:
Another way is to use two queries instead of one, and avoid the
len(set(...))
snippet:There is NO WAY to do actual calculated fields barring the provided aggregations, so either you do it in raw SQL or you do in code like this.
At least the proposed solution uses Django's ORM as far as possible.