categories.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:oscar-wagtail-demo 作者: LUKKIEN 项目源码 文件源码
def create_from_sequence(bits):
    """
    Create categories from an iterable
    """
    if len(bits) == 1:
        # Get or create root node
        name = bits[0]
        try:
            # Category names should be unique at the depth=2
            root = Category.get_root_nodes().get(title=name)
        except Category.DoesNotExist:
            root = Category.add_root(title=name)
        except Category.MultipleObjectsReturned:
            raise ValueError((
                "There are more than one categories with name "
                "%s at depth=1") % name)
        return [root]
    else:
        parents = create_from_sequence(bits[:-1])
        parent, name = parents[-1], bits[-1]

        try:
            child = parent.get_children().get(title=name)
        except (Page.DoesNotExist, Category.DoesNotExist):
            child = parent.add_child(title=name)
        except Category.MultipleObjectsReturned:
            raise ValueError((
                "There are more than one categories with name "
                "%s which are children of %s") % (name, parent))

        parents.append(child)
        return parents
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号