コード例 #1
0
def make_category_tree():
    top = Category(name='Top category',
                   slug=slugify('Top category'),
                   active=True)
    top.save()
    level1_first = Category(name='Level1 first',
                            slug=slugify('Level1 first'),
                            active=True,
                            parent=top)
    level1_first.save()
    level1_second = Category(name='Level1 second',
                             slug=slugify('Level1 second'),
                             active=True,
                             parent=top)
    level1_second.save()
    level2_first = Category(name='Level2 first',
                            slug=slugify('Level2 first'),
                            active=True,
                            parent=level1_first)
    level2_first.save()
    level2_first_sub = Category(name='Level2 first sub',
                                slug=slugify('Level2 first sub'),
                                active=True,
                                parent=level2_first)
    level2_first_sub.save()
    level2_second = Category(name='Level2 second',
                             slug=slugify('Level2 second'),
                             active=True,
                             parent=level1_first)
    level2_second.save()
    top_two = Category(name='Top category two',
                       slug=slugify('Top category two'),
                       active=True)
    top_two.save()
    level1_two_first = Category(name='Level1 two first',
                                slug=slugify('Level1 two first'),
                                active=True,
                                parent=top_two)
    level1_two_first.save()
    level1_two_second = Category(name='Level1 two second',
                                 slug=slugify('Level1 two second'),
                                 active=True,
                                 parent=top_two)
    level1_two_second.save()
    level1_two_second_sub = Category(name='Level1 two second sub',
                                     slug=slugify('Level1 two second sub'),
                                     active=True,
                                     parent=level1_two_second)
    level1_two_second_sub.save()
    Category.objects.rebuild()