2013-05-18 2 views
1

django-mptt 자습서를 성공적으로 완료했습니다. 내가 어떻게하는지 알 수없는 것은 아이의 아이를 만드는 것입니다.django-mptt를 사용하여 하위 레코드 만들기

어린이의 자녀는 제 3의 레벨 이상을 의미합니다. 나는 1.3.1, 1.3.2를 만들려면 아래 내가 insert_node있는 doco에서 1.3.1.1

1.0 Product Z 
     1.1 Product A 
     1.2 Product B 
     1.3 Product P 
      1.3.1 Product X 
        1.3.1.1 Product O 
      1.3.2 Product Y 
2.0 Product H 

을 예를 참조하지만이 작업을 진행하기에 충분한 이해 해달라고.

NOTE: This is a low-level method; it does NOT respect ``MPTTMeta.order_insertion_by``. 
     In most cases you should just set the node's parent and let mptt call this during save. 

내가 'insert_node'를 사용해야 하는가 또는 더 나은 방법이있다 : 또한 나는 말한다 insert_node에 관한 code comments (라인 317)에서 뭔가를 발견? 'insert_node'를 사용해야한다면 사용 예를 제공 할 수 있습니까?

답변

1

이것은 다소 혼란 스러울 수 있음을 인정합니다. 하지만 here을 읽을 수 있으므로 order_insertion_by 필드는 표준 삽입 동작과 같은 것, 예를 들어 사전 순으로 정렬 된 트리 등을 따를 때만 사용해야하며 추가 db 쿼리를 트리거합니다.

그러나 노드의 특정 지점에 노드를 삽입하려면 TreeManager.insert_node 또는 MPTTModel.insert_at 중 하나를 사용해야합니다. 후자는 첫 번째 호출에 편리한 방법입니다.

그래서, 당신의 예에 따라,이 1.3 제품 P의 새로운 1.3.3 제품 Q로 마지막 자식 추가하려면 다음 세 가지 옵션에 이르게 :

new_node = ProductNode(name='1.3.3 Product Q') 

parent = ProductNode.objects.get(name='1.3 Product P') 


# With `order_insertion_by` 

new_node.parent = parent 
new_node.save() 


# With `TreeManager.insert_node` 

ProductNode.objects.insert_node(new_node, parent, position='last-child', save=True) 


# With `MPTTModel.insert_at` 

new_node.insert_at(parent, position='last-child', save=True) 
관련 문제