The first step in every django project is prepping up the pieces of data you want to store. Every django project will have different apps, and those apps will all have different functionalities.
The following notes were taken in context to building an ecommerce app.
An ecommerce can have models like Products, Customers, Carts, Orders etc. Every model contains entities/attributes. See below.
- PRODUCTS
- Product
- Collection
- Tag
- CUSTOMERS
- Customer
- CARTS
- Cart
- CartItem
- ORDERS
- Order
- OrderItem
Organizing Models/Entities in Apps
-
We can have a single app, and then bundle all the entities inside that single app. Then we can bundle and distribute the app via pip, so that anybody can install the app and get all the models and the functionality around them. Therefore you can just get access to the models, thereby minimizing time to write extra code…and just customize the models based on your needs. See below
- STORE
- Product
- Collection
- Customer
- Cart
- CartITem
- Order
- OrderItem
A problem with this is that as the app grows and becomes more complex, it gets bloated with many things - like too many models etc. This is called a MONOLITH. It makes our app hard to understand, hard to maintain and hard to reuse. Each app should do one thing, and do it well.
- STORE
-
You can break down the project into 4 small apps - Products, Customers, Carts, Orders. Each app will handle different functionalities. This may be a very poor way of managing your app. Looking at the dependencies between the apps, Orders depend on Carts, Carts depend on Customers, Products depend on Carts. This means anytime you’re working with the project, you will have to install the products app, carts app, customers app and orders app. Ideally, each app should be self-contained..so it can easily be dropped into a new project.
-
Look above at the tag entity, the ability to tag products is optional. We don’t necessarily need it in an ecommerce app. We need it in other applications like a blog etc. Tagging is not really used in an ecommerce product, so its a different form of functionality. Therefore you can move the tags into a separate app called TAGS. And the TAGS app will contain the Tag entity and the TaggedItem entity.
- STORE
- Product
- Collection
- Customer
- Cart
- CartITem
- Order
- OrderItem
- TAGS
- Tag
- TaggedItem
With the above separation, each app is self-contained and provides a separate piece of functionality. So you can use either or both apps in a new project. There is zero coupling in these apps which means you can deploy and change them without affecting other apps.
- STORE
Therefore, we are going with the third option. So we create 2 new apps into our project. Store and Tags. See below
python manage.py startapp storepython manage.py startapp tagsAs usual, every time a new app is created, we should add it into the list of installed apps in our settings.py.