0

このDjango ブログ アプリカテゴリ システムを django-mptt に渡します。

私が抱えている問題は、_get_online_category方法についてです。このメソッドを使用すると、 を持つカテゴリのみを取得できますEntry

def _get_online_categories(self):
    """
    Returns categories with entries "online" inside.
    Access this through the property ``online_entry_set``.
    """
    from models import Entry
    return Category.objects.filter(entries__status=Entry.STATUS_ONLINE).distinct()

エントリを持つカテゴリを持つカテゴリも持つように変更するにはどうすればよいですか?

例えば ​​:

私は持っていてSpain > Malaga、マラガはEntry前の方法で手に入れました。私は両方を手に入れたいのですMalagaが、手に入れません。Spain

2 番目の質問:

親カテゴリからすべてのエントリを取得する方法は?

たとえば、スペインからマラガの投稿を取得しますか?

def _get_online_entries(self):
    """
    Returns entries in this category with status of "online".
    Access this through the property ``online_entry_set``.
    """
    from models import Entry        
    return self.entries.filter(status=Entry.STATUS_ONLINE)

online_entries = property(_get_online_entries)

スペインでは空の結果が返されます。

4

1 に答える 1

0

これは、最初の方法の良い解決策のようです:

def _get_online_categories(self):
    """
    Returns categories with entries "online" inside.
    Access this through the property ``online_entry_set``.
    """
    from models import Entry
    queryset =  self.categories.filter(entries__status=Entry.STATUS_ONLINE)

    new_queryset = queryset.none() | queryset
    for obj in queryset:
        new_queryset = new_queryset | obj.get_ancestors()
    return new_queryset

2 番目の質問

このようなものがそれになります:

def _get_online_entries(self):
    """
    Returns entries in this category with status of "online".
    Access this through the property ``online_entry_set``.
    """
    from models import Entry        
    return Entry.objects.filter(status=Entry.STATUS_ONLINE, 
                                category__lft__gte=self.lft, 
                                category__rght__lte=self.rght)

online_entries = property(_get_online_entries)
于 2012-03-09T11:31:40.083 に答える