python - Complex aggregation methods as single param in query string -


i’m trying design flexible api django rest. meant have field filterable through query string , in addition have param in query string can denote complex method perform. ok, here details:

views.py

class starsmodellist(generics.listapiview):     queryset = starsmodel.objects.all()     serializer_class = starsmodelserializer     filter_class = starsmodelfilter 

serializers.py

class starsmodelserializer(dynamicfieldsmixin, serializers.modelserializer):     class meta:         model = starsmodel         fields = '__all__' 

mixins.py

class dynamicfieldsmixin(object):     def __init__(self, *args, **kwargs):         super(dynamicfieldsmixin, self).__init__(*args, **kwargs)         fields = self.context['request'].query_params.get('fields')         if fields:             fields = fields.split(',')             # drop fields not specified in `fields` argument.             allowed = set(fields)             existing = set(self.fields.keys())             field_name in existing - allowed:                 self.fields.pop(field_name) 

filters.py

class csvfilter(django_filters.filter):     def filter(self, qs, value):         return super(csvfilter, self).filter(qs, django_filters.fields.lookup(value.split(u","), "in"))   class starsmodelfilter(django_filters.filterset):     id = csvfilter(name='id')      class meta:         model = starsmodel         fields = ['id',] 

urls.py

url(r’^/stars/$’, starsmodellist.as_view()) 

this give me ability construct query strings so:

/api/stars/?id=1,2,3&fields=type,age,magnetic_field,mass 

this great functionality, there many custom aggregation/transformation methods need applied data. have agg= param so:

/api/stars/?id=1,2,3&fields=type,age,magnetic_field,mass,&agg=complex_method 

or just:

/api/stars/?agg=complex_method 

where defining complex_method grabs correct fields job.

i’m not sure start , add complex methods appreciate guidance. should note api private use supporting django application, not exposed public.

definitely see mymodellist class anyway example per https://docs.djangoproject.com/en/1.10/ref/class-based-views/base/

from django.http import httpresponse django.views import view  class starsmodellist(generics.listapiview):     queryset = starsmodel.objects.all()     serializer_class = starsmodelserializer     filter_class = starsmodelfilter      def complex_method(request):         # smth input parameters if         return httpresponse('hello, world!')      def get(self, request, *args, **kwargs):         if request.get.get('agg', none) == 'complex_method':            return self.complex_method(request)         return httpresponse('hi, world!') 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -