42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
import random
|
||
from core.apps.api.models import Category
|
||
|
||
|
||
def generate_categories(total=150):
|
||
Category.objects.all().delete()
|
||
|
||
created = []
|
||
queue = []
|
||
|
||
# 1) Root level: 3–5 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 1–3 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 qo‘shamiz
|
||
queue.append(child)
|
||
|
||
return f"{len(created)} ta category yaratildi!"
|