[SOLVED] Custom view based on # of "documents.document_view" events per document

Technical aspects, customization, code samples.
Post Reply
lsmoker
Posts: 24
Joined: Wed Sep 05, 2018 3:52 pm

[SOLVED] Custom view based on # of "documents.document_view" events per document

Post by lsmoker »

If anyone has awesome Django-fu in this area, I'd appreciate it... As the topic says, I'd like to have a custom view that sorts descending by the number of times a document is viewed (I do have one currently that is based on a document metadata type which is updated when a document is viewed, but I'd like to have a view that has all of the logic within it, ie, calculates the document view count dynamically for the document).

The relationship from the Document model to the Action model (from the actstream package) is not by direct Foreign Key. If it was, the solution would be like this -> https://stackoverflow.com/questions/114 ... nkey-field. I'm not sure what it is... Generic FK?

Maybe this isn't possible or would hit the DB too hard...
Last edited by lsmoker on Wed Mar 03, 2021 3:24 pm, edited 1 time in total.
---
LeVon Smoker
lsmoker
Posts: 24
Joined: Wed Sep 05, 2018 3:52 pm

Re: Custom view based on # of "documents.document_view" events per document

Post by lsmoker »

Easier than I thought...

Document.valid.filter(target_actions__verb='documents.document_view').annotate(views=Count('target_actions')).order_by('-views')
---
LeVon Smoker
Post Reply