psychok7
psychok7

Reputation: 5473

Django Rest Framework organizing ViewSets and Routers

Hi there so my question is probably about best practices when organizing ViewSets and the corresponding Routers in Django Rest Framework.

According to the official documentation, the routers should be stored in the urls.py and the viewsets should be stored in views.py.

My idea of an approach would be having the viewsets in a separate file like for example viewsets.py so that we don't end up mixing the normal Django views and the DRF Viewsets in the same file, improving readability.

The same would go for the routers where we would creaate a file called routers inside each app and register then with the main Default router instance.

These are my thoughts, but i am not sure how to:

1º Do this the proper way (the registering of the viewsets routers and all, should i place DefaultROuter in the __init__.py ?)

2º Is there a better approach?

Basically i want to separate the logic per app and inside each app separate by django views and DRf viewsets

Upvotes: 0

Views: 1492

Answers (1)

Kevin Cherepski
Kevin Cherepski

Reputation: 1483

For what it's worth, I've always created a separate api/ subdirectory within my Django apps to hold all Django REST Framework related files. This is just one way of doing things but it's helped keep separation of concerns within our applications.

The hierarchy looked like this...

  • Django Project/
    • Django App/
      • views.py
      • models.py
      • urls.py
      • api/
        • serializers.py
        • viewsets.py

Upvotes: 4

Related Questions