I'm a newbie in Django Python and I need to create an API backend for a frontend done in React. I'm forced by the customer to use Django Python, no options on this!
The project is very simple: it needs to expose ~15 endpoints, use Django ORM to connect to a PostgreSQL database, and have basic business logic. I must have a minimum of 80% unit tests coverage of the code and expose a swagger UI.
My problem is to create a "standard", well-defined, wisely organized structure of the working directory structure as defined by the best practice.
In .NET I put my endpoints in the controllers, then I create the business logic with interfaces and the data layer where I have the model and the entities, so everything is all well organized and separated.
How can I achieve a similar organization in Django?
I've seen that some people use DRF (Django RESTful framework), is it good for my purpose? is it a best practice for projects like mine?
Django REST framework should work for your project. It supports the serialization, validation, authentication, and documentation (Swagger) needs for your ~15 endpoints that you are talking about.
Typical project Structure
my_project/
│
├── manage.py # Django's command-line utility
│
├── project_config/ # Project settings directory
│ ├── __init__.py
│ ├── settings.py # Project settings
│ ├── urls.py # Project-level URL routing
│ ├── asgi.py # ASGI config for deployment
│ └── wsgi.py # WSGI config for deployment
│
├── api/ # Main API app
│ ├── __init__.py
│ ├── urls.py # API-specific URL routing
│ ├── views/ # Your "controllers" in Django terms
│ │ ├── __init__.py
│ │ ├── user_views.py # User-related endpoints
│ │ ├── product_views.py # Product-related endpoints
│ │ └── ...
│ │
│ ├── serializers/ # Handle JSON conversion & validation
│ │ ├── __init__.py
│ │ ├── user_serializers.py
│ │ ├── product_serializers.py
│ │ └── ...
│ │
│ ├── models/ # Database models (ORM)
│ │ ├── __init__.py
│ │ ├── user.py
│ │ ├── product.py
│ │ └── ...
│ │
│ ├── services/ # Business logic layer
│ │ ├── __init__.py
│ │ ├── user_service.py
│ │ ├── product_service.py
│ │ └── ...
│ │
│ └── tests/ # Test directory
│ ├── __init__.py
│ ├── test_user_api.py
│ ├── test_product_api.py
│ └── ...
│
├── utils/ # Shared utilities and helpers
│ ├── __init__.py
│ ├── permissions.py # Custom permissions
│ ├── pagination.py # Custom pagination
│ └── ...
│
├── requirements.txt # Project dependencies
│
└── .env # Environment variables (not in version control)
This structure maps to your .NET terminology as follows.
Controllers
: In Django, these are called 'views' and live in the
api/views/ directory
.
Business Logic
: Your api/services/
directory is where business logic
lives.
Data Layer:
The Django ORM models
in api/models/
handle your data
layer.
.NET Layer | Django Equivalent |
---|---|
Controllers (API Endpoints) | api/views/ (Django Views) |
Business Logic (Services) | api/services/ |
Data Layer (Models) | api/models/ (Django ORM) |
Interfaces (Repositories, DTOs) | api/serializers/ (For DTOs), abstract base classes in utils/ |
Dependency Injection / Middleware | project_config/settings.py (for middleware), custom logic in utils/ |
Configuration / AppSettings | .env and project_config/settings.py |
Refer my blog that has steps once I made for my refrence for easy setup of Django API Backend Directory Structure. - Setting Up Your Django Project
Also a good refrence - https://plainenglish.io/blog/how-to-structure-your-django-project-a5d50333a644