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用以前的方法得到了一个,我只会得到,MalagaSpain我不想两者都有。

第二个问题:

如何从父类别中获取所有条目?

例如从西班牙获得马拉加的职位?

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

第二个问题

这样的事情会成功:

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 回答