2

What I want is to handle three ViewSet with one URL respect to the provided request.data.

Let's make it more clear by diving into code:

Here is my router.py

# vim: ts=4 sw=4 et

from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from . import viewsets

router = DefaultRouter()
router.register(r'endpoint', viewsets.MyViewSet, 'my-viewset')

notification_urls = [
    url(r'^', include(router.urls)),
]

Question1:

Is it possible to handle three ViewSet with one URL?

For example, according to the data that has been sent, I want to select dynamically a different ViewSet instead of assigned MyViewSet.

---> /endpoint/ ---- | request.data['platform'] == 1 ----> ViewSet1 
                     | request.data['platform'] == 2 ----> ViewSet2
                     | request.data['platform'] == 3 ----> ViewSet3

and I know that I can call other ViewSet's methods in a MyViewSet something like this Question but this is really a messy one.

Question2:

Is it possible to dynamically pass extra params respect to the data that has been sent to a ViewSet?

For example, according to the data that has been sent, I want to pass an extra param into MyViewSet.

I know that I can do all this stuff in ViewSet initial method, Like this:

def initial(self, request, *args, **kwargs):
    self.set_platform()
    super(NotificationViewSet, self).initial(request, *args, **kwargs)

But what I want is to handle this stuff during the MyViewSet instantiation.

NOTE: Maybe I can handle this by Writing a Middleware but if there are other options I would really be appreciated for sharing them with me.

Phoenix
  • 3,996
  • 4
  • 29
  • 40
  • If you implement this (somehow, ) it will be *tightly coupled code-base* which may lead difficulties in debugging. So, IMHO, this should be avoided – JPG May 03 '20 at 05:45
  • @ArakkalAbu thanks for warning me. I know this is against the rules and principles and best practices but this is some kind of business need and I have to find some way to implement it. If that was a common need I think Django would recommend some ways to implement it. So If you were me what would you do to implement it? – Phoenix May 03 '20 at 06:43

0 Answers0