Repository: GeotrekCE/Geotrek-admin

Issue:
API V2 - Limiter les champs renvoyés ne fonctionne pas pour Communes
Testé sur la route ``/city``. Que j'utilise la limitation à certains champs, ou le fait d'exclure certains champs, j'ai toujours tous les champs qui sont renvoyés par l'API.

Testé sur https://geotrekdemo.ecrins-parcnational.fr/api/v2/city/?fields=id%2Cname&omit=geometry&page=1&page_size=1
Idem sur la route ``/district``.

Par contre ça fonctionne sur la route ``/treks`` de limiter les champs : https://geotrekdemo.ecrins-parcnational.fr/api/v2/trek/?fields=ambiance&page=1&page_size=1

Soucis seulement sur la route des communes et des secteurs ? Ou autres ?


Discussion:

Resolving patch:
diff --git a/geotrek/api/v2/serializers.py b/geotrek/api/v2/serializers.py
--- a/geotrek/api/v2/serializers.py
+++ b/geotrek/api/v2/serializers.py
@@ -69,7 +69,7 @@ class Meta(BaseGeoJSONSerializer.Meta,
 
 
 if 'geotrek.trekking' in settings.INSTALLED_APPS:
-    class NetworkSerializer(serializers.ModelSerializer):
+    class NetworkSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         label = serializers.SerializerMethodField(read_only=True)
 
         def get_label(self, obj):
@@ -79,7 +79,7 @@ class Meta:
             model = trekking_models.TrekNetwork
             fields = ('id', 'label', 'pictogram')
 
-    class PracticeSerializer(serializers.ModelSerializer):
+    class PracticeSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         name = serializers.SerializerMethodField(read_only=True)
 
         def get_name(self, obj):
@@ -89,7 +89,7 @@ class Meta:
             model = trekking_models.Practice
             fields = ('id', 'name', 'order', 'pictogram',)
 
-    class TrekDifficultySerializer(serializers.ModelSerializer):
+    class TrekDifficultySerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         label = serializers.SerializerMethodField(read_only=True)
 
         def get_label(self, obj):
@@ -99,7 +99,7 @@ class Meta:
             model = trekking_models.DifficultyLevel
             fields = ('id', 'cirkwi_level', 'label', 'pictogram')
 
-    class RouteSerializer(serializers.ModelSerializer):
+    class RouteSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         route = serializers.SerializerMethodField(read_only=True)
 
         def get_route(self, obj):
@@ -110,7 +110,7 @@ class Meta:
             fields = ('id', 'pictogram', 'route')
 
 
-class ReservationSystemSerializer(serializers.ModelSerializer):
+class ReservationSystemSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
     class Meta:
         model = common_models.ReservationSystem
         fields = ('id', 'name')
@@ -124,7 +124,7 @@ class Meta:
         )
 
 
-class TargetPortalSerializer(serializers.ModelSerializer):
+class TargetPortalSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
     title = serializers.SerializerMethodField(read_only=True)
     description = serializers.SerializerMethodField(read_only=True)
     facebook_image_url = serializers.SerializerMethodField(read_only=True)
@@ -147,7 +147,7 @@ class Meta:
         )
 
 
-class RecordSourceSerializer(serializers.ModelSerializer):
+class RecordSourceSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
     class Meta:
         model = common_models.RecordSource
         fields = ('name', 'pictogram', 'website')
@@ -198,7 +198,7 @@ class Meta:
         )
 
 
-class LabelSerializer(serializers.ModelSerializer):
+class LabelSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
     name = serializers.SerializerMethodField(read_only=True)
     advice = serializers.SerializerMethodField(read_only=True)
 
@@ -234,7 +234,7 @@ class Meta:
                 'description_teaser', 'geometry', 'pictures', 'url'
             )
 
-    class InformationDeskTypeSerializer(serializers.ModelSerializer):
+    class InformationDeskTypeSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         label = serializers.SerializerMethodField(read_only=True)
 
         def get_label(self, obj):
@@ -244,7 +244,7 @@ class Meta:
             model = tourism_models.InformationDeskType
             fields = ('id', 'label', 'pictogram')
 
-    class InformationDeskSerializer(serializers.ModelSerializer):
+    class InformationDeskSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         type = InformationDeskTypeSerializer()
         name = serializers.SerializerMethodField(read_only=True)
         description = serializers.SerializerMethodField(read_only=True)
@@ -486,7 +486,7 @@ class Meta:
                 'update_datetime', 'url'
             )
 
-    class ThemeSerializer(serializers.ModelSerializer):
+    class ThemeSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         label = serializers.SerializerMethodField(read_only=True)
 
         def get_label(self, obj):
@@ -496,7 +496,7 @@ class Meta:
             model = trekking_models.Theme
             fields = ('id', 'label', 'pictogram')
 
-    class AccessibilitySerializer(serializers.ModelSerializer):
+    class AccessibilitySerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         name = serializers.SerializerMethodField(read_only=True)
 
         def get_name(self, obj):
@@ -582,7 +582,7 @@ class Meta:
             )
 
 if 'geotrek.zoning' in settings.INSTALLED_APPS:
-    class CitySerializer(serializers.ModelSerializer):
+    class CitySerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         geometry = geo_serializers.GeometryField(read_only=True, source="geom", precision=7)
         id = serializers.ReadOnlyField(source='code')
 
@@ -590,7 +590,7 @@ class Meta:
             model = zoning_models.City
             fields = ('id', 'geometry', 'name', 'published')
 
-    class DistrictsSerializer(serializers.ModelSerializer):
+    class DistrictsSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         geometry = geo_serializers.GeometryField(read_only=True, source="geom", precision=7)
 
         class Meta:
@@ -599,7 +599,7 @@ class Meta:
 
 
 if 'geotrek.outdoor' in settings.INSTALLED_APPS:
-    class RatingScaleSerializer(serializers.ModelSerializer):
+    class RatingScaleSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         name = serializers.SerializerMethodField(read_only=True)
 
         def get_name(self, obj):
@@ -609,7 +609,7 @@ class Meta:
             model = outdoor_models.RatingScale
             fields = ('id', 'name', 'practice')
 
-    class RatingSerializer(serializers.ModelSerializer):
+    class RatingSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         name = serializers.SerializerMethodField(read_only=True)
         description = serializers.SerializerMethodField(read_only=True)
 
@@ -623,7 +623,7 @@ class Meta:
             model = outdoor_models.Rating
             fields = ('id', 'name', 'description', 'scale', 'order', 'color')
 
-    class OutdoorPracticeSerializer(serializers.ModelSerializer):
+    class OutdoorPracticeSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         name = serializers.SerializerMethodField(read_only=True)
 
         def get_name(self, obj):
@@ -633,7 +633,7 @@ class Meta:
             model = outdoor_models.Practice
             fields = ('id', 'name')
 
-    class SiteTypeSerializer(serializers.ModelSerializer):
+    class SiteTypeSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
         name = serializers.SerializerMethodField(read_only=True)
 
         def get_name(self, obj):


Test patch: