On 05/19/2016 04:19 AM, Finucane, Stephen wrote:
On 18 May 22:30, Andy Doan wrote:

+class PatchListSerializer(ListSerializer):
+    '''Semi hack to make the list of patches more efficient'''
+    def to_representation(self, data):
+        del self.child.fields['content']
+        del self.child.fields['headers']
+        del self.child.fields['diff']
+        return super(PatchListSerializer, self).to_representation(data)

This works, but it is a hack like you say. I did some experimentation
and saw that defining 'exclude' via a Meta class doesn't work, sadly.
However, there do seem to be other ways to do this that might be worth
looking into, if you want:

     https://stackoverflow.com/q/22616973

The method there seems to require you to opt-in to every field you want to expose. We can do that, its just more work to say what we want to expose rather than what we don't want to expose.

+class PatchSerializer(ModelSerializer):
+    class Meta:
+        model = Patch
+        list_serializer_class = PatchListSerializer
+        read_only_fields = ('project', 'name', 'date', 'submitter', 'diff',
+                            'content', 'hash', 'msgid')
+
+    def to_representation(self, instance):
+        request = self.context.get('request', None)
+        data = super(PatchSerializer, self).to_representation(instance)
+
+        data['project'] = request.build_absolute_uri(
+            reverse('api_1.0:project-detail', args=[data['project']]))
+        data['submitter'] = request.build_absolute_uri(
+            reverse('api_1.0:person-detail', args=[data['submitter']]))
+        headers = data.get('headers')
+        if headers:
+            data['headers'] = email.parser.Parser().parsestr(headers, True)
+        return data

Again, this works but 'HyperLinkedModelSerializer' seems like a natural
fit. If you go this route, you should look at 'SerializerMethodField'
to handle 'headers'.

That didn't work well originally because:
 * my self-inflicted performance issues with related fields
 * "state" isn't exposed, just the string value

I think we can do some to_representation work to handle state, so I'll try and convert to HyperlinkedModels.

_______________________________________________
Patchwork mailing list
[email protected]
https://lists.ozlabs.org/listinfo/patchwork

Reply via email to