`target` value is always None. How can we set `tar

2019-05-30 16:36发布

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条回答
再贱就再见
2楼-- · 2019-05-30 17:17

Target is an optional property that may be used when adding activities. For this reason the property is always included on the activities returned when a feed is retrieved.

If you're looking to work with the field inside your Django models, you should be able to simply define "target" field (probably of type a CharField) on the Model.

Edit: To clarify, in order to have the 'target' field sent into Stream as part of the Activity, it needs be included via the extra_activity_data() function.

The fields automatically included from the model are actor, verb, object and time and shown in the Activity.create_activity() function (source).

查看更多
神经病院院长
3楼-- · 2019-05-30 17:28

I looked up getStream Activity class and there was no way to pass target instance.

For my case, I wanted to pass the target's content type and its 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

Thanks!

查看更多
登录 后发表回答