Files
backend/test.py

42 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import random
from core.apps.api.models import Category
def generate_categories(total=150):
Category.objects.all().delete()
created = []
queue = []
# 1) Root level: 35 ta
root_count = random.randint(3, 5)
for i in range(root_count):
cat = Category.objects.create(name=f"Category {len(created) + 1}")
created.append(cat)
queue.append(cat)
# 2) Qolganlarini yaratamiz
while len(created) < total:
if not queue:
break
parent = queue.pop(0)
# Har bir parentga 13 ta bola
children_count = random.randint(1, 3)
for _ in range(children_count):
if len(created) >= total:
break
child = Category.objects.create(
name=f"Category {len(created) + 1}",
parent=parent,
)
created.append(child)
# bola yana parent bo'lishi mumkin — shuning uchun queue ga qoshamiz
queue.append(child)
return f"{len(created)} ta category yaratildi!"