0

Right now I have a large viewset class (~1300 lines). The reason for this is, I had to add a lot of @action decorators to acheive my goal. But I can feel that this is taking a lot of my time in debugging and navigating the code. I want to know if there's a way to break this class into multiple files.

I don't know how to split a single into multiple classes and not sure if this is even possible.

1 Answer 1

0

I'd recommend an approach where you write multiple ViewSet classes, then register them separately with the router.

The multiple ViewSet classes can inherit from a common base class if there are shared methods.

Before:

router.register(r'my_api', MyLargeViewSet)

urlpatterns = [
    path('', include(router.urls)),
]

After:

router.register(r'my_api/related_apis_1', MySmallerViewSet1)
router.register(r'my_api/related_apis_2', MySmallerViewSet2)
router.register(r'my_api/related_apis_3', MySmallerViewSet3)

urlpatterns = [
    path('', include(router.urls)),
]

You could also use multiple routers, but that might be a bit overkill.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.