1

I use the BannerListAPIView when access the banner list:

class BannerListAPIView(ListAPIView):
    serializer_class = WebsiteBannerSerializer
    permission_classes = []
    queryset = WebsiteBanner.objects.all()

But I want to add my logic when access this ListAPIView, such as I want to record the remote_ip.

How to add my logic when I access this ListAPIView?


EDIT

I just want to know where I can add my custom logic when user access the ListAPIView.

vindev
  • 2,240
  • 2
  • 13
  • 20
qg_java_17137
  • 3,310
  • 10
  • 41
  • 84

2 Answers2

0

you can override the list() function of your class like so:

class BannerListAPIView(ListAPIView):
    serializer_class = WebsiteBannerSerializer
    permission_classes = []
    queryset = WebsiteBanner.objects.all()

    def list(self, *args, **kwargs):
        # YOUR LOGIC
        return super(BannerListAPIView, self).list(*args, **kwargs)
Dash Winterson
  • 1,197
  • 8
  • 19
  • `return super(WebsiteBannerSerializer, self).list(*args, **kwargs) TypeError: super(type, obj): obj must be an instance or subtype of type` Why there is a error? – qg_java_17137 Jan 17 '18 at 08:58
0

If you want to record IPs for every request app-wide, you may want to look into using a [middleware] rather than doing this at the view level, which may be considered bad practice.

Dash Winterson
  • 1,197
  • 8
  • 19