def prepare_schemes(**kwargs): attr = kwargs['attr'] #чистим дерево roots = Analytic.get_root_nodes() for root in roots: root.delete() #создаём все атрибутные схемы из AOTs schemes = AOT.objects.filter(attr=attr).values('schema').annotate(count=Count('schema')).order_by() #schemes = AOT.objects.filter(attr=attr, schema__id=1).values('schema').annotate(count=Count('schema')).order_by() for schema in schemes: schema = AS.objects.get(id=schema['schema'].__int__()) Analytic.add_root(nc_id=str(schema.id), text=schema.name, description=schema.description, type='AS', attr_schema_name=schema.name) #создаём схемы верхнего уровня(в том числе промежуточные) nodes = Analytic.objects.all() for node in nodes: schema = AS.objects.get(id=int(node.nc_id)) while schema.parent: try: Analytic.objects.get(nc_id=str(schema.parent.id)) except ObjectDoesNotExist: Analytic.add_root(nc_id=str(schema.parent.id), text=schema.parent.name, description=schema.parent.description, type='AS', attr_schema_name=schema.parent.name) schema = schema.parent #меняем parent для атрибутных схем nodes = Analytic.objects.all() for node in nodes: schema = AS.objects.get(id=int(node.nc_id)) if schema.parent: parent = Analytic.objects.get(nc_id=str(schema.parent.id)) node.parent = parent node.save() #update EXPANDED&LEAF nodes = Analytic.objects.all() for node in nodes: node.expanded = True node.leaf = False node.save()