Skip to content

Instantly share code, notes, and snippets.

@Anjaan-g
Last active August 12, 2021 17:52
Show Gist options
  • Save Anjaan-g/5f7fa493c6e3c3f0fcbd03f54ace0990 to your computer and use it in GitHub Desktop.
Save Anjaan-g/5f7fa493c6e3c3f0fcbd03f54ace0990 to your computer and use it in GitHub Desktop.
class MyViewSet(mixins.ListModelMixin, viewsets.GenericViewset):
queryset = MyModel.objects.select_related()
serializer_class = MyModelSerializer
@action(methods=['GET'], detail=False, url_path='some_path')
def get_something_done(self, request):
cache_data = Cache.get('something')
if cache_data:
return Response(cache_data, status=status.HTTP_200_OK)
queryset = self.filter_queryset(self.get_queryset())
df = queryset.to_dataframe() #It's to convert data in my django model to pandas dataframe
df = df.drop(['s_n','id'], axis=1)
result = get_something_done(df)
result = result.to_json(orient='records')
data = json.loads(result)
cache_data = Cache.set('something', data)
return Response(data, status=status.HTTP_200_OK)
@action(methods=['GET'], detail=False, url_path='another_path')
def get_another_thing_done(self, request):
cache_data = Cache.get('another_thing')
if cache_data:
return Response(cache_data, status=status.HTTP_200_OK)
queryset = self.filter_queryset(self.get_queryset())
df = queryset.to_dataframe()
df = df.drop(['s_n','id'], axis=1)
result = get_another_thing_done(df)
result = result.to_json(orient='records')
data = json.loads(result)
cache_data = Cache.set('another_thing', data)
return Response(data, status=status.HTTP_200_OK)
def get_something_done(*args):
'''do something here'''
return something
def get_another_thing_done(*args):
'''do somethingelse here'''
return another_thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment