Django活动流过滤器通过外键在目标模型中的操作 [英] Django activity stream filter Actions by foreignkey in target model
本文介绍了Django活动流过滤器通过外键在目标模型中的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此刻,我通过在所需的模型上生成模型流来抓取数据。
model_stream(FillingSystem)
我想扩展这个功能,并有这样的东西
model_stream(FillingSystem.objects.filter(client__slug ='my- slug'))
或
model_stream(FillingSystem.objects.filter(client = Client.objects.get(slug ='my-slug')))
这个模型看起来像这样:
class FillingSystem(models.Model) :
client = models.ForeignKey('accounts.Client')
如何过滤一个流由相关的slug字段?
解决方案
似乎你可以只是将您的过滤器作为 ** kwargs
:
model_stream(FillingSystem,filling_system__client__slug ='my-slug')
其中目标
是一个 GenericForeignKey
到您的内容(随意选择来自其他人)。
您可能需要声明与 Action
模型的反向关系:
<$ p $来自django.contrib.contenttypes.fields导入的p>
从actstream.models import导入GenericRelation
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')
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?
解决方案
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')
这篇关于Django活动流过滤器通过外键在目标模型中的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文