`target` value is always None. How can we set `target` value to certain instance in Django Mode? (GetStream)

时间:2017-12-18 06:48:51

标签: getstream-io

Here is notification that I got, and activities target is always None. How can we assign target instance in Model?

{'id': '6a4e1d107c07b2c7', 
...,
'activities': [
    EnrichedActivity(activity_data={'id': '6a4e1d18-e3b8-11e7-8080-80007c07b2c7', 'to': ['notification:1'], 'time': datetime.datetime(2017, 12, 18, 5, 58, 5, 508124), 'foreign_id': 'comment.Comment:22', 'object': <Follow: johnmatt3 : jbaek7023>, 'actor': <User: johnmatt3>, 
    'target': None, <-- ALWAYS None. Why? 
    'verb': 'follow', 'origin': None}, not_enriched_data={}), 
    EnrichedActivity(activity_data={'id': '62b0e89c-e3b8-11e7-8080-80017680d313', 'to': ['notification:1'], 'time': datetime.datetime(2017, 12, 18, 5, 57, 52, 733814), 'foreign_id': 'follow.Follow:21', 'object': <Follow: jbaek7023433 : jbaek7023>, 'actor': <User: jbaek7023433>, 'target': None, 'verb': 'follow', 'origin': None}, not_enriched_data={})], 
'is_seen': True, 'verb': 'comment'}, 

Here is Comment class. I guess there would be a certain property to set the 'target'

class Comment(models.Model, Activity):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='follower')
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    target = GenericForeignKey('content_type', 'object_id')

    @property
    def activity_notify(self):
        SomeModel = self.content_type.model_class()
        ...
    return []

I need the specific property which can handle which instance should the target. such as...

@property  
def activity_target_attr(self): # this is not working
    print('test')
    target = blah blah
    return target
# expected output... 
# {id... activities: [EnrichedActivity(
# 'id': ..., 'target': <Outfit 32> ...),...}

2 个答案:

答案 0 :(得分:2)

Target是可选属性,可在adding activities时使用。因此,该属性始终包含在检索订阅源时返回的活动中。

如果您希望使用Django模型中的字段,您应该能够在模型上简单地定义“目标”字段(可能是类型为CharField)。

修改:为了澄清,为了将“目标”字段作为活动的一部分发送到Stream,需要通过extra_activity_data()函数包含它。

模型中自动包含的字段为actorverbobjecttime,并显示在Activity.create_activity()函数中source )。

答案 1 :(得分:0)

我查找了getStream Activity类,没有办法传递target实例。

对于我的情况,我想传递目标的内容类型及其ID。

@property
def extra_activity_data(self):
    SomeModel = self.content_type.model_class()
    object_id = self.object_id
    qs = SomeModel.objects.filter(id=self.object_id)
    if qs:
        dict_object = '%s:%s' % (self.content_type, self.object_id)
        return {'target_address': dict_object}
    return None

谢谢!