def _bind_model(self):
        if self.groups[0].level != 0:
            self.log.info("Got invalid first group: {0}".format(self.groups[0]))
            raise ValueError("Invalid group tree: first group must have level of 0 (got {0})".format(self.groups[0].level))
        class Stack(list):
            def push(self, el):
                self.append(el)
        parent_stack = Stack([self.root])        
        current_parent = self.root
        prev_group = None
        for g in self.groups:
            g.db = self
            if prev_group is not None:
                if g.level > prev_group.level:
                    current_parent = prev_group
                    parent_stack.push(current_parent)
                elif g.level < prev_group.level:
                    while g.level <= current_parent.level:
                        current_parent = parent_stack.pop()
                    parent_stack.push(current_parent)
            g.parent = current_parent
            current_parent.children.append(g)
            prev_group = g
        for entry in self.entries:
            for group in self.groups:
                if entry.group_id == group.id:
                    group.entries.append(entry)
                    entry.group = group
                    break
            else:
                raise NotImplementedError("Orphaned entries not (yet) supported.")