django-project-structure/README.md

171 lines
6.2 KiB
Markdown
Raw Normal View History

2022-01-25 03:15:53 +00:00
# Django Project Structure
This is a template/project structure for developing django-based applications -
either strictly through the `django-rest-framework` or just `django`.
2022-01-18 02:27:09 +00:00
2022-08-15 04:51:06 +01:00
The project is meant to be easily clone-able, and used as the starter template for
2022-01-26 08:04:30 +00:00
the next big thing our team develops.
2022-08-09 06:04:56 +01:00
## Getting Started
2023-11-25 17:42:21 +00:00
1. Clone the repo from GitHub
2022-08-09 06:04:56 +01:00
1. Delete the `.git` folder
1. Remove/add anything as you see fit
1. Initialize as a new git repository for your own project
1. Run the project using `python manage.py runserver` and you should see the default
success page provided by Django at [http://127.0.0.1:8000/](http://127.0.0.1:8000/).
2022-08-09 06:04:56 +01:00
### Creating an App
1. Create a folder with the app name in `apps`. For example: `poll`
1. Run `python manage.py startapp poll apps/poll` from the root directory of the project
2022-01-26 08:12:22 +00:00
## Scope
2022-08-09 06:04:56 +01:00
- Goal: Develop a structure for both `django-rest-framework` and `django` projects.
2022-08-15 04:51:06 +01:00
- Easily clone-able when starting new projects
2022-08-09 06:04:56 +01:00
- Folder structure only, not “best practices”
- We wont be enforcing about best practices in implementation details
- Well hold all questions in a GitHub repository, so things can move forward
- The discussions also serve historic significance - people can backtrack why some decisions were made.
2022-01-26 08:12:22 +00:00
2022-01-26 08:04:30 +00:00
## Project Tree
```bash
.
├── apps
2022-08-09 06:04:56 +01:00
│ └── example_api # A django rest app
│ ├── api
│ │ ├── v1
│ │ │ ├── __init__.py
│ │ │ ├── serializers.py
│ │ │ ├── services.py
│ │ │ ├── tests.py
│ │ │ ├── urls.py
│ │ │ └── views.py
│ │ ├── v2
│ │ │ ├── __init__.py
│ │ │ ├── serializers.py
│ │ │ ├── services.py
│ │ │ ├── tests.py
│ │ │ ├── urls.py
│ │ │ └── views.py
│ │ └── __init__.py
│ ├── management
│ │ ├── commands
│ │ │ └── command.py
│ │ └── __init__.py
2022-01-26 08:04:30 +00:00
│ ├── migrations
│ │ └── __init__.py
│ ├── templates
2022-08-09 06:04:56 +01:00
│ ├── tests
2022-01-26 08:04:30 +00:00
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── models.py
2022-01-26 08:12:22 +00:00
│ ├── urls.py
2022-08-09 06:04:56 +01:00
│ ├── utils.py
2022-01-26 08:04:30 +00:00
│ └── views.py
2022-08-15 04:34:17 +01:00
├── common # An optional folder containing common "stuff" for the entire project
2022-01-26 08:04:30 +00:00
├── config
2022-08-15 04:38:11 +01:00
│ ├── settings.py
2022-01-26 08:04:30 +00:00
│ ├── asgi.py
│ ├── __init__.py
│ ├── urls.py
│ └── wsgi.py
├── deployments
│ ├── django-project
│ │ └── Dockerfile
│ ├── nginx
│ │ ├── default.conf
│ │ └── Dockerfile
│ └── docker-compose.yml
├── docs
│ ├── CHANGELOG.md
│ ├── CONTRIBUTING.md
│ ├── deployment.md
│ ├── local-development.md
│ └── swagger.yaml
├── requirements
│ ├── common.txt
│ ├── development.txt
│ ├── local.txt
│ └── production.txt
├── static
├── entrypoint.sh
├── manage.py
├── pytest.ini
└── README.md
2022-01-25 03:15:53 +00:00
```
2022-01-26 08:12:22 +00:00
2022-01-26 08:04:30 +00:00
## Rationale
2022-08-15 04:51:06 +01:00
Each `app` should be designed in way to be plug-able, that is, dragged and dropped
2022-01-26 08:12:22 +00:00
into any other project and itll work independently.
2022-01-26 08:04:30 +00:00
### `apps`
2022-08-09 06:04:56 +01:00
* A mother-folder containing all apps for our project. Congruent to any JS-framework's `src` folder.
* An app can be a django template project, or an API.
2022-01-25 03:15:53 +00:00
2022-08-09 06:04:56 +01:00
### `api`
2022-01-26 08:12:22 +00:00
* We like to place all our API components into a package within an app called
2022-08-09 06:04:56 +01:00
`api`. For example, in this repository it's the `example_api/api` folder. That allows us to isolate our API components in a consistent location. If
2022-01-26 08:12:22 +00:00
we were to put it in the root of our app, then we would end up with a huge list
of API-specific modules in the general area of the app.
For projects with a lot of small, interconnecting apps, it can be hard to hunt
down where a particular API view lives. In contrast to placing all API code
within each relevant app, sometimes it makes more sense to build an app
specifically for the API. This is where all the serializers, renderers, and views
are placed. Therefore, the name of the app should reflect its API version
2022-08-09 06:04:56 +01:00
### `api-versioning`
2022-04-14 13:50:07 +01:00
It might often be necessary to support multiple versions of an API throughout the lifetime of a project. Therefore, we're adding in support right from the start.
For different API versions, we're assuming the following will change:
- Serializers
- Views
- URLs
- Services
2022-08-09 06:04:56 +01:00
`model`s can be thought of as shared between versions. Therefore, migrating changes should be versioned carefully without breaking different versions of the API.
2022-01-25 03:15:53 +00:00
2022-04-14 13:57:18 +01:00
2022-08-09 06:04:56 +01:00
### What's Version 2?
2022-04-14 13:57:18 +01:00
Currently we're proposing that major changes to the following, constitutes a new API version:
1. Representation of data, either for submission or retrieval
2022-08-09 06:04:56 +01:00
1. Major optimizations
1. Major code reorganization and code refactor
2022-04-14 13:57:18 +01:00
2022-01-26 08:04:30 +00:00
### `config`
* Contains project configuration files, including the primary URL file
2022-08-15 04:38:11 +01:00
* ~~Contains settings split into `base`, `local`, `production` and `development`.~~. Update: As environment
specific variables will be handled using environment variables, we've deemed it unnecessary to have
separate settings files.
2022-01-25 03:15:53 +00:00
2022-01-26 08:04:30 +00:00
### `deployments`
2022-08-09 06:04:56 +01:00
* Contains Docker, Docker-Compose and nginx specific files for deploying in different
2022-01-26 08:04:30 +00:00
environments
2022-01-25 03:15:53 +00:00
2022-01-26 08:12:22 +00:00
### `documentation`
* Well have CHANGELOG.md
* Well have CONTRIBUTING.md
* Well have deployment instructions
2022-08-09 06:04:56 +01:00
* Well have local startup instructions
2022-01-26 08:12:22 +00:00
### `services`
* Well be writing business logic in services instead of anywhere else.
### `gitignore`
* https://github.com/github/gitignore/blob/main/Python.gitignore
2022-01-25 03:15:53 +00:00
## References
2022-01-26 08:04:30 +00:00
- [Two Scoops of Django by Daniel and Audrey Feldroy](https://www.feldroy.com/books/two-scoops-of-django-3-x)
- [Django Best Practices](https://django-best-practices.readthedocs.io/en/latest/index.html)
- [Cookiecutter Django](https://github.com/cookiecutter/cookiecutter-django)
- [HackSoft Django Style Guide](https://github.com/HackSoftware/Django-Styleguide)
- [Radoslav Georgiev - Django Structure for Scale and Longevity](https://www.youtube.com/watch?v=yG3ZdxBb1oo)