Search code examples
djangorestweb-servicesdjango-rest-frameworkapi-design

How to organize Django API


I'm creating an API using Django Rest Framework for a project.

I'm fairly new to Django and to DRF, and really to the development of APIs in general, so I'm having some doubts on how to organize my project.

I have an app for the API, and for the purpose of organization I separated the views and serializers by files for each resource, sort of like this:

|- api
 |- views
  |- resource1.py
  |- resource2.py
  ...
 |- serializers
  |- resource1.py
  |- resource2.py
  ...

Meanwhile I was told that the APIs for each resource would probably be reused in other contexts, so it would be beneficial if these "modules" were independent.

How should I structure this project so that the APIs for each resource can be reused easily in the future, independently from each other? My first instinct would be to create and App inside this project for each resource. Does this make sense?

Thank you!


Solution

  • According to the doc, you should create different app by using the command :

    python manage.py startapp yourapp
    

    For Django, an app

    is a Web application that does something – e.g., a Weblog system, a database of public records or a small poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

    So in your case, you should create multiple apps and for each app, you will have a serializer file, a view file and a model file (thanks to the command line). Follow your first instinct ;) Like you said, you can have at the root a module folder to reuse some part.