我正在使用GetStream Django包与getstream.io进行交互.我已经成功使用Enricher()
该类来丰富我的活动源与Django模型信息feed_manager.get_user_feed()
,但无法得到类似的结果feed_manager.get_notification_feed()
这是我的模型的缩短版本.
class Task(models.Model, Activity): title = models.CharField() assigned_to = models.ForeignKey(User) created_at = models.DateTimeField(auto_now_add=True) created_by = models.ForeignKey(User) @property def activity_object_attr(self): return self @property def activity_actor_attr(self): return self.assigned_to @property def activity_notify(self): return [feed_manager.get_notification_feed(self.assigned_to.id),]
如果我的视图抓取用户Feed:
enricher = Enrich() feed = feed_manager.get_user_feed(request.user.id) # feed = feed_manager.get_notification_feed(request.user.id) activities = feed.get(limit=25)['results'] enriched_activities = enricher.enrich_activities(activities)
我的输出按预期工作,并且每个输出都填充了我的模板中的正确数据:
Actor: {{ activity.actor }}
Title: {{ activity.title }}
Time: {{ activity.time|timesince }}
但是,如果我切换到通知订阅源(请注意注释行外的更改):
enricher = Enrich() # feed = feed_manager.get_user_feed(request.user.id) feed = feed_manager.get_notification_feed(request.user.id) activities = feed.get(limit=25)['results'] enriched_activities = enricher.enrich_activities(activities)
然后,我可以从中获取数据的唯一字段是activity.created_at
.
输出activity.keys
显示如下:
[u'activities', u'group', u'activity_count', u'created_at', u'updated_at', u'actor_count', u'verb', u'is_seen', u'id', u'is_read']
似乎对于通知提要,演员和对象不会被报告回GetStream:
但是,它是针对用户提要的:
我很难过为什么.我错过了什么?
好的......所以我弄清楚了我的错误.我一直在假设这notification_feed
是一个扁平的饲料.哎呀,糟透了.它实际上是聚合的.因此,我能够修复如下:
代替:
activities = enricher.enrich_activities(activities)
我用了:
enriched_activities = enricher.enrich_aggregated_activities(activities)
然后,在我的基本模板中:
{% for enriched_activity in enriched_activities %} {% render_activity enriched_activity %} {% endfor %}
在/activity/aggregated/task.html中查找
{% for activity in enriched_activity.activities %} {% render_activity activity %} {% endfor %}
最后在/activity/task.html里面,我看到了这些项目的预期输出.
Actor: {{ activity.actor.first_name }}
Title: {{ activity.object.title }}
Time: {{ activity.time|timesince }}