How to serialize the array of specific field of cl

2019-08-07 09:12发布

问题:

I'm trying to push EnrichedActivity data to FeedSerializer. I'm getting feed data using getStream

class FeedAPIView(APIView):
    def get(self, request, format=None):
        feeds = feed_manager.get_news_feeds(request.user.id)
        # get the newsfeed for user.
        activities = feeds.get('timeline').get()['results']
        activities = enricher.enrich_activities(activities)
        # I want to serialize only Outfit instances(object) from the activities.
        filtered_set = FeedSerializer(activities.getObject.convertToQueryset)
        json_output = { "feed" : filtered_list }
        return Response(json_output, status=status.HTTP_200_OK)

Here is the array data from activities

[
    EnrichedActivity(
        activity_data={
            'target': None, 'verb': 'outfit', 'actor': <User: georgiamason522>, 
            'to': [], 'foreign_id': 'outfit.Outfit:23',
            'id': 'x7x7bxxxxxxxxxxx3xbxx', 
            'object': <Outfit: georgiamason522 23>, // Data
            'time': datetime.datetime(2017, 12, 14, 1, 36, 6, 833617), 'origin': 'user:17'}, 
            not_enriched_data={}), 
    EnrichedActivity(..., 'object': <Outfit: georgiamason522 22>,
]

I want to get this queryset from the array above,

filtered_set = ???
print(filtered_set)
<QuerySet [<Outfit: georgiamason522 23>, <Outfit: georgiamason522 22>]>

To put queryset into FeedSerializer.

FeedSerializer(filtered_set, many=True, context={'request': self.context['request']}).data

My question is...

  1. EnrichedActivity data seems like class object. How do we extract only activity_data > object.

  2. Convert Extracted object data to the Queryset

Here is pseudo code which can be right.

# This is Pseudo Code in my mind! This is not correct or can be not even close
filtered_set = [activity.activity_data.object].getQueryset for activity in activities

Please comment below if anything is not clear or need more information.

UPDATE

class FeedSerializer(serializers.ModelSerializer):
    comments = serializers.SerializerMethodField()
    ...
    is_owner = serializers.SerializerMethodField()

    class Meta:
        model = Outfit # I want to serialize Outfit Model instance 
                       # from feed in enriched list

    ...

Outfit model // you can think of it as like normal post

class Outfit(models.Model, Activity):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
    ...

    @property
    def get_content_type(self):
        instance = self
        content_type = ContentType.objects.get_for_model(instance.__class__)
        return content_type

    @property
    def activity_object_attr(self):
        return self

    @property
    def activity_notify(self):
        targets = [feed_manager.get_news_feeds(self.user.id)['timeline']]

回答1:

Here is my solution. Thanks to... @DwightGunning

    feed = []
    for activity in activities:
        outfit_instance = activity.__dict__.get('activity_data').get('object')
        data = FeedSerializer(
            outfit_instance,
            context={'request': request}).data
        feed.append(data)

    json_output = { "feed" : feed }