Page 1 of 1

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

Posted: Wed Mar 03, 2021 3:01 pm
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...

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

Posted: Wed Mar 03, 2021 3:24 pm
by lsmoker
Easier than I thought...

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