BestSellerMetrics Model
The BestSellerMetrics model tracks sales metrics and rankings for products, including total sales, weekly sales, and monthly sales.
Fields
| Field | Type | Required | Description |
|---|---|---|---|
product |
OneToOneField | Yes | Reference to the associated Product model. |
total_sales |
PositiveIntegerField | No | Total sales of the product. |
weekly_sales |
PositiveIntegerField | No | Sales in the past 7 days. |
monthly_sales |
PositiveIntegerField | No | Sales in the past 30 days. |
last_sale_date |
DateTimeField | No | Date and time of the last sale. |
rank_overall |
PositiveIntegerField | No | Overall rank based on total sales. |
rank_weekly |
PositiveIntegerField | No | Weekly rank based on weekly sales. |
rank_monthly |
PositiveIntegerField | No | Monthly rank based on monthly sales. |
updated_at |
DateTimeField | No | Timestamp when the record was last updated. |
Custom Methods
update_sales_metrics(product_id, quantity)-
Updates sales metrics in real-time when a purchase occurs. It adjusts the total, weekly, and monthly sales based on the quantity sold.
-
calculate_metrics() -
This method calculates and updates the rankings for products based on their sales metrics.
-
update_ranks() - Updates the rankings for products in the system, calculating overall, weekly, and monthly ranks using a
DenseRankwindow function.
Meta Options
db_table:'tbl_bestseller_metrics'- Indexes:
-total_sales-weekly_sales-monthly_sales
SalesMetrics Model
The SalesMetrics model tracks the sales performance of products for a vendor over a specific time period (daily, weekly, monthly, or yearly). It stores metrics like total sales, total quantity sold, new customers, and total orders for a given product and vendor.
Fields
| Field | Type | Required | Description |
|---|---|---|---|
vendor |
ForeignKey (Vendor) | No | Reference to the associated Vendor model. |
product |
ForeignKey (Product) | No | Reference to the associated Product model. |
date |
DateField | Yes | The date for which the sales data is being recorded. |
total_sales |
DecimalField | No | Total sales for the given date, vendor, and product. |
total_quantity |
PositiveIntegerField | No | Total quantity of the product sold on the given date. |
new_customers |
PositiveIntegerField | No | Number of new customers who made purchases on the given date. |
total_orders |
PositiveIntegerField | No | Total number of orders for the given date. |
filter_type |
CharField | No | The filter type (daily, weekly, monthly, or yearly). |
Custom Methods
update_metrics(vendor_id, product_id, sales_amount, quantity_sold, new_customers, orders_count, filter_type)- Updates or creates a sales metric entry for a given vendor, product, and date.
- It increments the
total_sales,total_quantity,new_customers, andtotal_ordersbased on the provided values. - The method uses
get_or_createto either update an existing record or create a new one if it doesn’t exist for the given date, vendor, product, and filter type.
Meta Options
db_table:'tbl_sales_metrics'- Indexes:
date-total_salesvendor, dateproduct, date- Unique Together: Ensures that each combination of
date,vendor,product, andfilter_typeis unique.