Django Models Deep Dive
Fix the Broken Order ModelHardFix the Code
4/5
Fix Buggy Code

Description

The following Order model has **several bugs and anti-patterns**. Your task is to identify and fix all the issues. The model should represent an order with line items, a status workflow, and a computed total.
This model has 5 issues: wrong field types, missing constraints, an incorrect property, a bad default, and a Meta class problem.

Issues to Fix

01Fix the price field — FloatField causes rounding errors for currency
02Fix the status field — it should use TextChoices, not a plain CharField with no constraints
03Fix the `total` property — it currently doesn't use `F()` expressions and would fail on empty orders
04Fix the `created_at` field — it uses `auto_now` but should use `auto_now_add`
05Fix the `unique_together` — it should prevent duplicate products in the same order

Example

# Fixed version should:
# - Use DecimalField for price
# - Use TextChoices for status
# - Use aggregate + Coalesce for total
# - Use auto_now_add for created_at
# - Have unique_together = ['order', 'product_name']
Python 3Contains bugs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Ln 36, Col 1