1

I would like to filter Actions for particular queryset.

To this moment I was grabbing data by generating a model stream on desired model.

model_stream(FillingSystem)

I would like to extend this functionality and have something like this

model_stream(FillingSystem.objects.filter(client__slug='my-slug')) 

or

model_stream(FillingSystem.objects.filter(client=Client.objects.get(slug='my-slug'))) 

this model looks like this:

class FillingSystem(models.Model):
    client = models.ForeignKey('accounts.Client')

How do I filter a stream by related slug field?

Efrin
  • 2,323
  • 3
  • 25
  • 45
  • This should be the answer: http://stackoverflow.com/questions/7339580/filter-through-a-related-model-django – skywalker Jan 10 '16 at 17:59

1 Answers1

3

It seems you can just pass your filters as **kwargs:

model_stream(FillingSystem, filling_system__client__slug='my-slug')

where target is a GenericForeignKey to your content (feel free to choose from the others).

You may have to declare a reverse relation to the Action model:

from django.contrib.contenttypes.fields import GenericRelation
from actstream.models import Action

class FillingSystem(models.Model):
    client = models.ForeignKey('accounts.Client')
    stream_actions = GenericRelation(
                         Action,
                         content_type_field='target_content_type'
                         object_id_field='target_object_id'
                         related_query_name='filling_system')
Alex Morozov
  • 5,823
  • 24
  • 28
  • Field 'target' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation. – Efrin Jan 10 '16 at 18:07
  • There seems to be still the same error :) client = models.ForeignKey('accounts.Client') stream_actions = GenericRelation(Action) – Efrin Jan 10 '16 at 18:16
  • @Efrin, sorry for that, GFK's are a bit messy. Try specifying all that stuff manually as I did in the (updated one more time) answer. Just pay attention to the changed `kwarg`. – Alex Morozov Jan 10 '16 at 18:23
  • Wow! That was awesome, thank you very much :) You saved me a lot of time! :) – Efrin Jan 10 '16 at 18:26
  • 1
    This doesn't work for me, it fails with ```operator does not exist: character varying = integer LINE 1: ... ON ("actstream_action"."action_object_object_id" = "notific... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.``` target_object_id uses CharField, while the key of my other model is a normal Integer. Any ideas? – graup Feb 26 '16 at 15:01
  • Stumbling upon the same error as @graup - any suggestions? – Hybrid May 19 '16 at 01:57
  • 1
    @Hybrid I raised the issue here: https://github.com/justquick/django-activity-stream/issues/281 The author is aware of it. – graup May 22 '16 at 18:53
  • I see, thank you. I ended up just creating a custom plugin for myself which accepts the object_id as a `PositiveInteger()` instead of a `CharField()` (which I don't know why the author would do in the first place)... but I assume the casting error could be fixed by simply wrapping the `object_id` with `str()` function. – Hybrid May 24 '16 at 19:04