71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
"""
|
|
Test script to create category hierarchy for testing the tree view
|
|
"""
|
|
import os
|
|
import django
|
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
|
|
django.setup()
|
|
|
|
from products.models import ProductCategory
|
|
|
|
print("Creating test category hierarchy...")
|
|
print("="*60)
|
|
|
|
# Get existing categories
|
|
existing = list(ProductCategory.objects.all()[:3])
|
|
|
|
if len(existing) >= 3:
|
|
cat1, cat2, cat3 = existing[0], existing[1], existing[2]
|
|
|
|
# Make sure they are root categories first
|
|
cat1.parent = None
|
|
cat2.parent = None
|
|
cat3.parent = None
|
|
cat1.save()
|
|
cat2.save()
|
|
cat3.save()
|
|
|
|
print(f"Root categories:")
|
|
print(f" 1. {cat1.name} (id={cat1.pk})")
|
|
print(f" 2. {cat2.name} (id={cat2.pk})")
|
|
print(f" 3. {cat3.name} (id={cat3.pk})")
|
|
|
|
# Create child for cat2 (if doesn't exist)
|
|
child1, created = ProductCategory.objects.get_or_create(
|
|
name="Test Child 1",
|
|
defaults={'parent': cat2, 'is_active': True}
|
|
)
|
|
if not created:
|
|
child1.parent = cat2
|
|
child1.save()
|
|
|
|
print(f"\nChild category:")
|
|
print(f" - {child1.name} (id={child1.pk}, parent={cat2.name})")
|
|
|
|
# Create grandchild for child1 (if doesn't exist)
|
|
grandchild1, created = ProductCategory.objects.get_or_create(
|
|
name="Test Grandchild 1",
|
|
defaults={'parent': child1, 'is_active': True}
|
|
)
|
|
if not created:
|
|
grandchild1.parent = child1
|
|
grandchild1.save()
|
|
|
|
print(f"\nGrandchild category:")
|
|
print(f" - {grandchild1.name} (id={grandchild1.pk}, parent={child1.name})")
|
|
|
|
print("\n" + "="*60)
|
|
print("Hierarchy created successfully!")
|
|
print("\nExpected tree structure:")
|
|
print(f"{cat1.name}")
|
|
print(f"{cat2.name}")
|
|
print(f" {child1.name}")
|
|
print(f" {grandchild1.name}")
|
|
print(f"{cat3.name}")
|
|
print("\nVisit http://127.0.0.1:8000/products/categories/ to see the tree view")
|
|
|
|
else:
|
|
print("Not enough categories to create hierarchy")
|
|
print("Please create at least 3 categories first")
|