Django REST Framework - multiple models / APIs?

We use Django to create a backend for web applications, providing a RESTful API for the Ember application.

So (evolutionarily) we started with the following simple structure:

project root | |-app1 / models.py .... no views.py | |-app2 / models.py .... no views.py | |-app3 / models.py .... no views.py | \- restapi - provides REST API for app*: huge views.py, huge serializers.py, huge test.py 

It is easy to use, especially with DRF viewing preview:

 @api_view(['GET']) def api_root(request, format=None): return Response( { 'users': reverse('current-user-detail', request=request), 'interfacesettings': reverse('interface-settings', request=request), ............................................................ 'preferences': reverse('preferences', request=request), } ) 

Soon, we have enough models / APIs to make our restapi.app too complex and messy, and we began to consider using something more logical:

 project root | |-app1 / models.py .... views.py, serializers.py, tests.py | |-app2 / models.py .... views.py, serializers.py, tests.py | |-app3 / models.py .... views.py, serializers.py, tests.py | \- we do not need rest api anymore (but where will we put our api_root?) 

On the other hand, we have all the complex tests (which include several models) in one place, which is convenient. And we reuse serialization functions. And we have one api_root. Maybe we could have something like this:

 project root | |-app1 / models.py .... views.py (app1 API), serializers.py, tests.py | |-app2 / models.py .... views.py (app2 API), serializers.py, tests.py | |-app3 / models.py .... views.py (app3 API), serializers.py, tests.py | \- restapi - views.py (api_root), tests.py for complicated tests and serializers.py for common functions 

Which approach is better? And what are the common best practices here? Is there any open source project we can watch?

+6
source share
1 answer

I am creating a restful API also with Django, like you. What I'm doing is applications that are independent, like your last example.

We have the β€œmain” django application, where we have api_root in the views and a file called β€œapis_urls.py”, where we organize all the URLs from different applications.

We also have in this "main" application the file "apis_filters.py", where we have filters that can use any application with the API and "apis_permissions.py" to control permissions to use apis and import to another application.

So in the end we work like this:

 project root | |-app1 / models.py .... views.py (app1 API), serializers.py, tests.py | |-app2 / models.py .... views.py (app2 API), serializers.py, tests.py | |-app3 / models.py .... views.py (app3 API), serializers.py, tests.py | \- core / views.py (api_root), apis_urls.py, apis_filters.py, apis_permissions.py 

We conduct all tests in the corresponding application.

And having apis_urls.py, we can have all the API URLs, for example:

 http://localhost/api/v1/example1 http://localhost/api/v1/example2 

In the main urls.py we have:

 url(r'^api/v1/', include('core.apis_urls', namespace='api')), 

And in the main application, on the page "apis_urls.py":

 urlpatterns = patterns( '', ####### Users Urls url(r'^users/$', users_views.UserListAPIView.as_view(), name='users-list-api'), url(r'^me/$', users_views.LoggedUserRetrieveUpdateAPIView.as_view(), name='logged-user-detail-api'), url(r'^users/(?P<username>[\ w.@ +-]+)/$', users_views.UserRetrieveAPIView.as_view(), name='users-detail-api'), ####### Comments Urls url(r'^comments/(?P<pk>[0-9]+)$', comments_api_views.CommentCreateAPIView.as_view(), name='comments-create-api'), ) 

I hope this help is how I found it more organized.

+9
source

All Articles