开发者

Rewriting "SELECT DISTINCT ON ..." using Django's ORM

开发者 https://www.devze.com 2023-01-16 22:16 出处:网络
I am using the following model with Django: class Hit(Model): image = ForeignKey(Image) user = ForeignKey(User)

I am using the following model with Django:

class Hit(Model):
    image = ForeignKey(Image)
    user = ForeignKey(User)
    timestamp = DateTimeField(auto_now_add = True)

What I need is basically a list that contains the count of "first hits" (i.e. hits with no earlier timestamp for the same image) for every user to create sort of a rank list.

Or still easier, just a list that contains a user name one time for every time this user has made a "first hit".

In SQL using the PostgreSQL "DISTINCT ON" extension, this would be a simple query like:

SELECT DISTINCT ON (image_id) user_id FROM proj_hit ORDER BY image_id ASC, timestamp ASC;

It there a way, to get this result with Dja开发者_运维百科ngo's ORM or (at least) portable SQL, i.e. no PostgreSQL extensions?


Are you at liberty to make a change to your model? It would help to de-normalize and store the first hit information in the same model or as part of a different model.

For e.g.

class Hit(Model):
    image = ForeignKey(Image)
    user = ForeignKey(User)
    timestamp = DateTimeField(auto_now_add = True)
    is_first_hit = BooleanField(default = False)

You can then override the save() method (or tap a signal) to set the is_first_hit explicitly on save. This would make inserts and updates a little more expensive but would make querying very easy.


I'm pretty sure that the portable SQL version is very similar to the version that you posted — simply drop the ON:

SELECT DISTINCT image_id, user_id FROM proj_hit ORDER BY image_id ASC, timestamp ASC;
0

精彩评论

暂无评论...
验证码 换一张
取 消