Repository: readthedocs/readthedocs.org

Issue:
Move `expandable_fields` to meta class
We are using the old way, declaring `expandable_fields` at the class level instead of on the meta class.

Docs on https://github.com/rsinger86/drf-flex-fields

This file needs to be updated https://github.com/readthedocs/readthedocs.org/blob/master/readthedocs/api/v3/serializers.py

Ref https://github.com/readthedocs/readthedocs.org/pull/6158#discussion_r322472534


Discussion:
only the `serializers.py` needs to be updated?
@Iamshankhadeep if you find other files, feel free to update those too

Resolving patch:
diff --git a/readthedocs/api/v3/serializers.py b/readthedocs/api/v3/serializers.py
--- a/readthedocs/api/v3/serializers.py
+++ b/readthedocs/api/v3/serializers.py
@@ -115,15 +115,6 @@ class BuildSerializer(FlexFieldsModelSerializer):
     state = BuildStateSerializer(source='*')
     _links = BuildLinksSerializer(source='*')
 
-    expandable_fields = dict(
-        config=(
-            BuildConfigSerializer,
-            dict(
-                source='config',
-            ),
-        ),
-    )
-
     class Meta:
         model = Build
         fields = [
@@ -140,6 +131,10 @@ class Meta:
             '_links',
         ]
 
+        expandable_fields = {
+            'config': (BuildConfigSerializer, {'source': 'config'})
+        }
+
     def get_finished(self, obj):
         if obj.date and obj.length:
             return obj.date + datetime.timedelta(seconds=obj.length)
@@ -215,15 +210,6 @@ class VersionSerializer(FlexFieldsModelSerializer):
     urls = VersionURLsSerializer(source='*')
     _links = VersionLinksSerializer(source='*')
 
-    expandable_fields = dict(
-        last_build=(
-            BuildSerializer,
-            dict(
-                source='last_build',
-            ),
-        ),
-    )
-
     class Meta:
         model = Version
         fields = [
@@ -241,6 +227,12 @@ class Meta:
             '_links',
         ]
 
+        expandable_fields = {
+            'last_build': (
+                BuildSerializer, {'source': 'last_build'}
+            )
+        }
+
     def get_downloads(self, obj):
         downloads = obj.get_downloads()
         data = {}
@@ -448,18 +440,6 @@ class ProjectSerializer(FlexFieldsModelSerializer):
     created = serializers.DateTimeField(source='pub_date')
     modified = serializers.DateTimeField(source='modified_date')
 
-    expandable_fields = dict(
-        active_versions=(
-            VersionSerializer,
-            dict(
-                # NOTE: this has to be a Model method, can't be a
-                # ``SerializerMethodField`` as far as I know
-                source='active_versions',
-                many=True,
-            ),
-        ),
-    )
-
     class Meta:
         model = Project
         fields = [
@@ -489,6 +469,18 @@ class Meta:
             '_links',
         ]
 
+        expandable_fields = {
+            'active_versions': (
+                VersionSerializer,
+                {
+                    # NOTE: this has to be a Model method, can't be a
+                    # ``SerializerMethodField`` as far as I know
+                    'source': 'active_versions',
+                    'many': True,
+                }
+            )
+        }
+
     def get_homepage(self, obj):
         # Overridden only to return ``None`` when the project_url is ``''``
         return obj.project_url or None


Test patch: