Commit 2d9acbbf authored by Florent Chehab's avatar Florent Chehab

API tweaks

parent c8583636
Pipeline #22054 failed with stage
in 1 minute and 31 seconds
......@@ -88,7 +88,23 @@ class CountrySerializer(serializers.ModelSerializer):
'region', 'regions')
class CountryViewSet(viewsets.ModelViewSet):
class DictModeViewSet(viewsets.ModelViewSet):
"""
ViewSet that renders data as dict with keys corresponding to the model
primary key. Instead of list.
"""
def list(self, request, *args, **kwargs):
response = super(viewsets.ModelViewSet, self).list(
request, *args, **kwargs) # call the original 'list'
pk_attr_name = self.serializer_class.Meta.model._meta.pk.name
response.data = {
d[pk_attr_name]: d for d in response.data
}
return response
class CountryViewSet(DictModeViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Country.objects.all() # noqa: E1101
serializer_class = CountrySerializer
......@@ -116,7 +132,7 @@ class RegionSerializer(serializers.ModelSerializer):
fields = ('name', 'un_code', 'parent', 'parent_url', 'region_type')
class RegionViewSet(viewsets.ModelViewSet):
class RegionViewSet(DictModeViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Region.objects.all() # noqa: E1101
serializer_class = RegionSerializer
......@@ -129,15 +145,15 @@ class CitySerializer(serializers.ModelSerializer):
return serializers.HyperlinkedRelatedField(
view_name='country-detail',
read_only=True) \
.get_url(obj.country, view_name='region-detail',
.get_url(obj.country, view_name='country-detail',
request=self.context['request'], format=None)
class Meta:
model = City
fields = ('name', 'local_name', 'area', 'country', 'country_url')
fields = ('name', 'local_name', 'area', 'country', 'country_url', 'id')
class CityViewSet(viewsets.ModelViewSet):
class CityViewSet(DictModeViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = City.objects.all() # noqa: E1101
serializer_class = CitySerializer
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment