Django activity stream filter Actions by foreignkey in target model

六月ゝ 毕业季﹏ 提交于 2019-12-08 03:16:57

问题


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?


回答1:


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')


来源:https://stackoverflow.com/questions/34708719/django-activity-stream-filter-actions-by-foreignkey-in-target-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!