Repository navigation
Expand file tree
/
Copy pathpopulate_sample_data_simple.py
More file actions
103 lines (86 loc) · 3.8 KB
/
Copy pathpopulate_sample_data_simple.py
File metadata and controls
103 lines (86 loc) · 3.8 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Lightweight version without image generation
Run with: python manage.py shell < populate_sample_data_simple.py
"""
import os
import sys
import random
from decimal import Decimal
from datetime import timedelta
import django
# Setup Django environment
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecommerce.settings')
django.setup()
from django.utils import timezone
from faker import Faker
from products.models import Category, ProductType, Product, ProductSpec # Change to your app name
fake = Faker()
def create_sample_data():
"""Create sample data without images"""
print("Creating sample data...")
# Clear existing data
Category.objects.all().delete()
ProductType.objects.all().delete()
# Create product types
product_types = []
for name in ["Simple", "Configurable", "Digital", "Bundle"]:
pt = ProductType.objects.create(
name=f"{name} Product",
slug=name.lower(),
has_variants=(name in ["Configurable", "Bundle"]),
digital_only=(name == "Digital")
)
product_types.append(pt)
# Create categories
laptops = Category.objects.create(name="laptops", slug="laptops")
desktops = Category.objects.create(name="desktops", slug="desktops")
components = Category.objects.create(name="components", slug="components")
# Create subcategories
# Category.objects.create(name="Smartphones", slug="smartphones", parent=laptops)
# Category.objects.create(name="Laptops", slug="laptops", parent=laptops)
Category.objects.create(name="gpus", slug="gpus", parent=components)
# Category.objects.create(name="Women's Clothing", slug="womens-clothing", parent=desktops)
# Category.objects.create(name="Furniture", slug="furniture", parent=components)
# Category.objects.create(name="Kitchen", slug="kitchen", parent=components)
# Sample products
products_data = [
{"name": "Premium Laptop", "price": 1299.99, "category": "laptops"},
{"name": "Premium Desk", "price": 1599.99, "category": "desktops"},
# {"name": "Smartphone Pro", "price": 899.99, "category": "smartphones"},
{"name": "RTX-550", "price": 149.99, "category": "gpus"},
{"name": "Fan", "price": 99.99, "category": "components"},
{"name": "Cheap PC", "price": 179.99, "category": "desktops"},
]
# Create products
for data in products_data:
category = Category.objects.get(slug=data["category"])
product = Product.objects.create(
name=data["name"],
slug=data["name"].lower().replace(" ", "-"),
description=fake.paragraph(nb_sentences=3),
category=category,
product_type=random.choice(product_types),
price=Decimal(str(data["price"])),
rating=random.randint(3, 5),
created_at=timezone.now() - timedelta(days=random.randint(0, 14))
)
# Add some specifications
specs = [
{"key": "Brand", "value": fake.company()},
{"key": "Color", "value": random.choice(["Black", "White", "Silver", "Blue"])},
{"key": "Warranty", "value": random.choice(["1 Year", "2 Years"])},
]
for spec in specs:
ProductSpec.objects.create(
product=product,
key=spec["key"],
value=spec["value"]
)
print(f"Created {Category.objects.count()} categories")
print(f"Created {ProductType.objects.count()} product types")
print(f"Created {Product.objects.count()} products")
print(f"Created {ProductSpec.objects.count()} product specifications")
print("\nSample data created successfully!")
if __name__ == "__main__":
create_sample_data()