I am using the following model with Django:
class Hit(Model):
image = ForeignKey(Image)
user = ForeignKey(User)
timestamp = DateTimeField(auto_now_add = True)
I basically need a list containing the number of “first hits” (ie hits with an earlier timestamp for the same image) for each user to create a list of ranks.
Or even simpler, just a list that contains the username once for each time that user made the “first hit”.
In SQL, using the PostgreSQL extension "DISTINCT ON", this will be a simple query like:
SELECT DISTINCT ON (image_id) user_id FROM proj_hit ORDER BY image_id ASC, timestamp ASC;
Is there a way to get this result with Django ORM or (at least) portable SQL, i.e. no PostgreSQL extensions?
source
share