Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

orm

django

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 Django's ORM or (at least) portable SQL, i.e. no PostgreSQL extensions?

like image 294
ChrisM Avatar asked Jul 02 '26 04:07

ChrisM


1 Answers

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.

like image 63
Manoj Govindan Avatar answered Jul 03 '26 19:07

Manoj Govindan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!