Database Schema
Relevant source files
The following files were used as context for generating this wiki page:The Becathlon database schema is the structural backbone of the e-commerce platform, designed using Django's Object-Relational Mapper (ORM). It encompasses several interconnected modules (Django apps) to manage users, product catalogs, shopping carts, orders, and physical store information. The schema is normalized to ensure data integrity and is centered around the User model, which links to profiles, products, carts, and orders.
This document outlines the key data models and their relationships across the different applications. The core entities include Product, User, Profile, Cart, Order, and Store, each residing in its respective application module.
Entity-Relationship Diagram
The following diagram provides a high-level overview of the main entities in the Becathlon database and their relationships.
This ER diagram illustrates the core relationships, such as a USER having a PROFILE and a CART, and placing an ORDER which contains ORDER_ITEMs corresponding to PRODUCTs.
Sources: apps/main/models.py, apps/profiles/models.py, apps/catalog/models.py, apps/cart/models.py, apps/order/models.py, apps/stores/models.py
User and Profile Management
This section covers the models responsible for user data, authentication, and profiles. The system uses Django's built-in User model and extends it with Profile and Customer models.
Sources: apps/profiles/models.py, apps/main/models.py
User (django.contrib.auth.models)
The standard Django User model is used for authentication and serves as the central point for linking to other models like Profile, Product, Order, and Cart.
Profile
The Profile model extends the User model with a one-to-one relationship to store additional user-specific information, including the crucial account_type which differentiates between a 'BUYER' and a 'SELLER'.
This diagram shows the one-to-one link between the User and Profile models.
Sources: apps/profiles/models.py:7-25
| Field | Type | Description |
|---|---|---|
user | OneToOneField (User) | A one-to-one link to the Django User model. |
account_type | CharField | Defines the user's role. Choices are 'BUYER' or 'SELLER'. Default is 'BUYER'. |
bio | TextField | A short biography for the user. Optional. |
location | CharField | The user's location. Optional. |
birth_date | DateField | The user's date of birth. Optional. |
profile_picture | URLField | URL for the user's profile picture. Optional. |
created_at | DateTimeField | Timestamp when the profile was created. Auto-set on creation. |
updated_at | DateTimeField | Timestamp of the last update. Auto-set on save. |
Sources: apps/profiles/models.py:7-25
Customer
The Customer model also extends the User model and appears to be a legacy or alternative profile model, storing a phone number and address.
| Field | Type | Description |
|---|---|---|
user | OneToOneField (User) | A one-to-one link to the Django User model. |
phone_number | CharField | The customer's phone number. Optional. |
address | TextField | The customer's address. Optional. |
Sources: apps/main/models.py:44-51
Product Catalog
The product catalog is managed through models in the apps/main and apps/catalog applications. It includes product categories, the products themselves, and associated images and reviews.
Sources: apps/main/models.py, apps/catalog/models.py
ProductType
This model represents the categories for products, such as "Running", "Cycling", etc.
| Field | Type | Description |
|---|---|---|
name | CharField | The unique name of the product category (e.g., "Running Shoes"). |
description | TextField | A description of the category. Optional. |
Sources: apps/main/models.py:8-15
Product
This is the central model for all products sold on Becathlon. It contains details like name, price, stock, and is linked to a ProductType and the User who created it.
| Field | Type | Description |
|---|---|---|
name | CharField | The name of the product. |
description | TextField | A detailed description of the product. |
price | DecimalField | The price of the product. |
product_type | ForeignKey (ProductType) | The category this product belongs to. |
stock | PositiveIntegerField | The available stock quantity. Defaults to 0. |
image_url | URLField | A URL for the product's main image. Optional. |
created_by | ForeignKey (User) | The user (seller) who created the product. |
brand | CharField | The brand of the product. Optional. |
rating | DecimalField | The average rating of the product. Defaults to 0.00. |
created_at | DateTimeField | Timestamp of creation. |
updated_at | DateTimeField | Timestamp of the last update. |
Sources: apps/main/models.py:18-41
ProductImage & ProductReview
These models provide additional details for a Product. ProductImage allows for multiple images per product, while ProductReview stores user-submitted ratings and comments.
This diagram shows that a PRODUCT can have multiple PRODUCT_IMAGEs and PRODUCT_REVIEWs.
Sources: apps/catalog/models.py:7-27
ProductImage Fields
| Field | Type | Description |
|---|---|---|
product | ForeignKey (Product) | The product this image is associated with. |
image_url | URLField | The URL of the image. |
alt_text | CharField | Alternative text for the image. Optional. |
is_primary | BooleanField | Indicates if this is the main image for the product. Defaults to False. |
| Sources: apps/catalog/models.py:8-13 |
ProductReview Fields
| Field | Type | Description |
|---|---|---|
product | ForeignKey (Product) | The product being reviewed. |
user | ForeignKey (User) | The user who wrote the review. |
rating | PositiveSmallIntegerField | A rating from 1 to 5. |
comment | TextField | The text content of the review. Optional. |
created_at | DateTimeField | Timestamp when the review was created. |
| Sources: apps/catalog/models.py:18-27 |
Shopping Cart
The apps/cart module manages the shopping cart functionality for authenticated users.
Sources: apps/cart/models.py
Cart and CartItem
A Cart is created for each user, and it contains multiple CartItems, each representing a specific product and its quantity.
This diagram shows the structure of the shopping cart, linking a USER to a CART which holds CART_ITEMs.
Sources: apps/cart/models.py:7-30
Cart Fields
| Field | Type | Description |
|---|---|---|
user | OneToOneField (User) | A one-to-one link to the user who owns the cart. |
created_at | DateTimeField | Timestamp of cart creation. |
updated_at | DateTimeField | Timestamp of the last cart modification. |
| Sources: apps/cart/models.py:8-12 |
CartItem Fields
| Field | Type | Description |
|---|---|---|
cart | ForeignKey (Cart) | The cart this item belongs to. |
product | ForeignKey (Product) | The product added to the cart. |
quantity | PositiveIntegerField | The quantity of the product in the cart. Defaults to 1. |
| Sources: apps/cart/models.py:15-20 |
Order Management
The apps/order module is responsible for handling customer orders, including shipping details, order status, and the items within each order.
Sources: apps/order/models.py
ShippingAddress
Stores shipping addresses for users. A user can have multiple addresses.
| Field | Type | Description |
|---|---|---|
user | ForeignKey (User) | The user who owns this address. |
address_line_1 | CharField | The main address line. |
city | CharField | The city. |
state | CharField | The state or province. |
postal_code | CharField | The postal code. |
country | CharField | The country. |
is_default | BooleanField | True if this is the user's default shipping address. |
| Sources: apps/order/models.py:11-23 |
Order and OrderItem
The Order model represents a completed transaction. It is linked to a user and a shipping address. Each Order contains multiple OrderItems, which are snapshots of the products at the time of purchase.
Order Field | Type | Description |
|---|---|---|
order_id | UUIDField | A unique identifier for the order. |
user | ForeignKey (User) | The user who placed the order. |
shipping_address | ForeignKey (ShippingAddress) | The address where the order will be shipped. |
total_price | DecimalField | The total cost of the order. |
status | CharField | The current status of the order (e.g., 'Pending', 'Shipped', 'Delivered'). |
created_at | DateTimeField | Timestamp of order creation. |
updated_at | DateTimeField | Timestamp of the last status update. |
| Sources: apps/order/models.py:26-92 |
OrderItem Field | Type | Description |
|---|---|---|
order | ForeignKey (Order) | The order this item belongs to. |
product | ForeignKey (Product) | The product that was ordered. |
quantity | PositiveIntegerField | The quantity of the product ordered. |
price | DecimalField | The price of the product at the time of order. |
| Sources: apps/order/models.py:95-103 |
Stores
The apps/stores module manages information about physical store locations.
Sources: apps/stores/models.py
Store
This model contains all the necessary information for a physical store, such as its name, location, and contact details.
| Field | Type | Description |
|---|---|---|
name | CharField | The name of the store. |
address | TextField | The full address of the store. |
latitude | DecimalField | The geographical latitude of the store. |
longitude | DecimalField | The geographical longitude of the store. |
phone_number | CharField | The store's contact phone number. Optional. |
store_hours | CharField | The opening and closing hours of the store. Optional. |
country | CharField | The country where the store is located. Optional. |
created_at | DateTimeField | Timestamp of creation. |
updated_at | DateTimeField | Timestamp of the last update. |
Sources: apps/stores/models.py:7-22
Summary
The database schema for Becathlon is a well-structured system that logically separates concerns into different Django applications. It effectively models the core concepts of an e-commerce platform: users with distinct roles (Profile), a categorized product catalog (Product, ProductType), a persistent shopping cart (Cart, CartItem), a comprehensive order management system (Order, OrderItem), and physical store information (Store). The use of foreign keys and one-to-one relationships ensures data integrity and establishes clear connections between the different parts of the application.