Cart Models Documentation
This documentation provides details on the Cart and CartItem models used for managing user shopping carts in the application.
Cart Model
The Cart model represents a shopping cart for a user. Each user can have one cart containing multiple items.
Fields
| Field | Type | Required | Description |
|---|---|---|---|
user |
ForeignKey(User) | Yes | Reference to the User model, representing the user who owns the cart. |
created_at |
DateTimeField | No | Timestamp of when the cart was created. |
updated_at |
DateTimeField | No | Timestamp of when the cart was last updated. |
Properties
total_items: Returns the total number of items in the cart.subtotal: Returns the total price of the cart items, excluding any discounts.total_discount: Returns the total discount applied to the cart.final_total: Returns the final total after applying the discount to the subtotal.
CartItem Model
The CartItem model represents an individual product added to a user's shopping cart. Each CartItem can optionally refer to a product variant.
Fields
| Field | Type | Required | Description |
|---|---|---|---|
cart |
ForeignKey(Cart) |
Yes | Reference to the Cart model, representing the cart the item belongs to. |
product |
ForeignKey(Product) |
Yes | Reference to the Product model, representing the product in the cart. |
variant |
ForeignKey(ProductVariant) |
No | Reference to the ProductVariant model, representing a variant of the product. |
quantity |
PositiveIntegerField |
Yes | The quantity of the product in the cart. |
is_customized |
BooleanField |
No | Indicates whether the product is customized. |
customized_fields |
JSONField |
No | The fields representing customization options, stored as a JSON object. |
is_direct_purchase |
BooleanField |
No | Whether the product is being purchased directly. |
has_purchased |
BooleanField |
No | Whether the product has been purchased. |
created_at |
DateTimeField |
No | Timestamp when the item was added to the cart. |
updated_at |
DateTimeField |
No | Timestamp when the cart item was last updated. |
Meta
db_table: The table name is set totbl_carts_items.constraints: Ensures that each combination ofcart,product, andvariantis unique ifhas_purchasedisFalse.
Properties
unit_price: Returns the price of the product or its variant. If the variant is unavailable, it returns the price of the product.total_price: Returns the total price for the quantity of the product in the cart.discount_amount: Returns the discount amount applied to the cart item based on the product's discount percentage.
Methods
clean(): Validates the cart item, ensuring the product variant belongs to the selected product, is available, and does not exceed the available stock.save(): Overrides the default save method to validate the item before saving it to the database.
Validation Errors
The following validation errors may be raised:
Variant does not belong to the selected product: Raised if the variant does not belong to the selected product.Selected variant is not available: Raised if the variant is not available.Only {stock_quantity} items available in stock: Raised if the quantity exceeds the available stock.