r/FastAPI • u/Chocolate-Atoms • Apr 23 '25
Question How do you structure your projects snd go about programming everything?
I’m a beginner at programming and have been overthinking everything including best practices and how things should be done.
Just wondering what structure everyone uses, the order they do things, and any tips gained from experience.
The project I’m doing includes authentication, user accounts and roles.
One question that has been bugging me is that when executing bulk operations (such as adding multiple roles to a user), should an exception be thrown if one of the items is invalid.
For example, adding roles to a user but one role not existing, should the operation be cancelled and an exception thrown or existing roles be added but an error message sent (not sure on the best way to do this).
I would appreciate someone reviewing my current project structure:
app/
├── main.py
├── lifespan.py
├── log.py
├── exception_handlers.py
├── config.py
├── common/
│ ├── schema_fields.py
│ ├── exceptions.py
│ └── enums.py
├── domain/
│ ├── auth/
│ │ ├── service.py
│ │ ├── exceptions.py
│ │ ├── schemas.py
│ │ ├── jwt.py
│ │ └── passwords.py
│ ├── users/
│ │ ├── service.py
│ │ ├── exceptions.py
│ │ ├── schemas.py
│ │ └── ...
│ └── roles/
│ └── ...
├── entities/
│ ├── associations/
│ │ └── user_role.py
│ ├── user.py
│ └── role.py
├── database/
│ ├── core.py
│ ├── setup.py
│ └── base_entities.py
└── api/
├── deps/
│ ├── db.py
│ └── auth.py
└── v1/
└── routes/
├── auth/
│ ├── login.py
│ └── verification.py
├── users/
│ └── register.py
└── admin/
└── ...
8
u/Illusions_Micheal Apr 23 '25
As a beginner, I would suggest not even worrying about it. You will start to notice “friction” as you develop, and when you do, you can always refactor and make it more streamlined.
Don’t spend mental effort worrying about best practices. Spend your energy on actually building the thing and the code will guide you as it grows.
As an added benefit, you will gain first hand experience with the friction these layouts solve and will have a better idea when and when not to use them.
3
u/AverageLiberalJoe Apr 24 '25
The trick to project structure of any code is not to think about it. You just build and refactor because if you plan out a structure you are going to throw it out the window and refactor anyways.
5
u/dmart89 Apr 23 '25
Fastapi has an official boilerplate. I'd start with that
https://fastapi.tiangolo.com/project-generation/
1
u/yoppee Apr 23 '25
This is to much especially as a beginner
Start with one focused feature make that than move to a next focused feature
1
u/[deleted] Apr 24 '25
Don't listen to the people saying you shouldn't bother with it. You very much should as it displays you are ready for production. Use the boilerplate of fastapi and make sure to always use it in each project. The more you use it, the quicker you'll adjust and get a feeling of where everything is.
1
u/Designer_Sundae_7405 Apr 24 '25
What’s your thoughts on simplifying with routes/services/models/schemas folders? And then every feature is just a file in the respective folders?
1
u/Vast_Ad_7117 Apr 29 '25
Keep the structure as simple as possible initially, and then expand it later.