Search code examples
djangodjango-activity-stream

Get all Actions filtered by Verb with Django Activity Stream


I'm quite new to Django and Python, so maybe my Question might be stupid but..

I'm logging what users do on my page with Django-Activity-Stream.

And now I'm trying to build a page where I'll have a nice graph showing me what users did depending on the action and date.

So far so well, until now I did it with a raw sql query but since Django's not supposed to be used like this, I want to count actions filtered by the verb it's allocated to.

My raw query looks like:

cursor = connection.cursor()
cursor.execute("SELECT COUNT(id) FROM actstream_action WHERE verb = %s "
"AND '2018-06-01' <= timestamp AND '2018-06-30' >= timestamp", ['User created'])
row = cursor.fetchone()
return row[0]

I hope somebody could help me! Thanks in advance


Solution

  • You could import the actstream.models.Action class and query on it.

    For example, to get a queryset of all actions with the verb added new post that was generated between 20th and 24th September 2018, you could do this

    import datetime
    from actstream.models import Action
    
    start = datetime.datetime(2018, 9, 20)
    end = datetime.datetime(2018, 9, 24)
    actions = Action.objects.filter(verb='added new post', timestamp__gt=start, timestamp__lt=end)