2012-09-11 1 views
3

Django 응용 프로그램에서 Article 모델로 django-mptt를 사용하고 있습니다. 예를 들어 숨김으로 설정된 모든 기사를 얻으려는 경우트리를 깨지 않고 django-mptt 필터

Article.objects.filter(hidden=False) 

그러나 mptt-tree가 손상 될 수 있습니다. 어떻게 모든 숨겨진 아티클을 제외 할뿐만 아니라 그 노드의 자식도 그런 식으로 트리의 무결성을 유지하면서 tree_info 템플릿 태그를 사용할 수있는 방식으로 쿼리 세트에서 필터링 할 수 있습니까?

+0

어떤 식으로 쿼리가 트리를 "중단"합니까? –

+0

그러면 노드가 부모 노드를 잃을 수 있습니다. :영형 – Triplr

답변

1

비슷한 문제가 있습니다. 노드와 그 모든 자식을 제거하고 싶습니다.

class FolderForm(forms.ModelForm): 
    class Meta: 
     model = Folder 
     fields = ('name', 'parent') 

    def __init__(self, *args, **kwargs) 
     super(FolderForm, self).__init__(*args, **kwargs) 

     if self.instance is not None: 
      exclude_ids = [f.id for f in self.instance.get_descendants(
       include_self=True)] 

      self.fields['parent'].queryset = self.fields['parent'].queryset \ 
       .exclude(pk__in=exclude_ids) 
0

이 간단한 해결책이있다 : 여기

내가 그렇게 할 관리하는 방법입니다 단지뿐만 아니라 숨겨진 모든 아이들을 설정합니다.