Commit c1914a04 authored by Florent Chehab's avatar Florent Chehab

DictModeViewSet moved to new folder for cleaner code

parent 6222ef22
Pipeline #26279 failed with stages
in 1 minute and 24 seconds
from django.db import models
from rest_framework import serializers, viewsets, permissions
from rex.models.tools import DictModeViewSet
# Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/
......@@ -29,25 +30,6 @@ class City(models.Model):
API RELATED STUFF BELLOW
"""
# TODO move somewhere else
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( # pylint: disable=E1003
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 CountrySerializer(serializers.ModelSerializer):
class Meta:
......
from rest_framework import viewsets
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( # pylint: disable=E1003
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
from .DictModeViewSet import DictModeViewSet # noqa: F401
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