English 中文(简体)
Django admin + MPTT: How to show children whenfiling a parent node?
原标题:Django admin + MPTT : How to display children when filtering a parent node?

I set up 2 models Article and Taxonomy. Taxonomy is an MPTTModel Subclass.

除一件事外,所有工作都受到罚款: 如果我选择一个没有相关条款的母子的话,就不会展示与儿童节点相关的文章。

因此,我正试图将我的习俗过滤器编码,但我却坐在盘点过滤器上。 我如何过滤这一问询,以显示与儿童相关的所有文章。

class TaxonomyFilter(SimpleListFilter):
    """
    Return incremented taxonomy list. Filtering by parent node display all children.
    """
    title = ( Index )
    parameter_name =  node_id 

    def lookups(self, request, model_admin):
        taxos = Taxonomy.objects.all()
        leafs = []
        for taxo in taxos:
            leafs.append((taxo.id,(taxo.name)))

        return leafs


    def queryset(self,request,queryset):
        """
        Filter list by displaying children if parent node has any.
        """
        if self.value():
            return queryset.filter()  
        else:
            return queryset
最佳回答

Juli: 这在水平上下了两点,但问题不及解决(而不是3)的问题。

def queryset(self, request, queryset):
    """
    Filter list by displaying children if parent node has any.
    """
    t = Taxonomy.objects.get(pk=self.value())

    return queryset.filter(taxonomy__lft__gte=t.lft, taxonomy__rght__lte=t.rght)
问题回答

不能确定回答我们自己的问题,但对于在此面临同样问题的任何人来说,“工作证据,而不是子弹证据”是 s。

    def queryset(self,request,queryset):
    """
    Filter list by displaying children if parent node has any.
    """
    descendants = Taxonomy.objects.get(pk=self.value()).get_descendants(include_self=True)

    descendant_list = []
    for descendant in descendants:
        descendant_list.append(descendant.id)

    return queryset.filter(taxonomy__in=descendant_list)

With django-mptt there are some cases where the query as described by craigds might not work, as the left and right leaf attributes are not unique in any way. To avoid false matches you can use the tree_id field that comes with django-mptt:

t = Taxonomy.objects.get(pk=self.value())

return queryset.filter(
    taxonomy__tree_id=t.tree_id,
    taxonomy__lft__gte=t.lft,
    taxonomy__rght__lte=t.rght
)




相关问题
Column/field level permissions in Django admin site?

Is it possible to implement column level permissions per user in the Django admin site? Its a small project and I only need two groups of permissions. In the docs I cant find anything out of the box ...

Django: Serving admin media files

I am trying to serve static files from another domain (sub domain of current domain). To serve all media files I used this settings: MEDIA_URL = http://media.bud-inform.co.ua/ So when in ...

Django ValueError at /admin/

I am running Django with mod_python on a Red Hat Linux box in production. A little while ago, for a reason unknown to me, the admin stopped working, throwing a 500 error. The error is as follows: ...

Django ordered ManyToManyField in admin interface

I have a legacy database with tables for documents and authors. A third table defines an ordered many to many relationship between the documents and authors, using foreign keys to the documents and ...

Django Admin - Re-authentication?

I m in a bit of a dilemma at the moment regarding Django s admin backend. The default authentication system allows already logged-in users that have staff privileges to access the admin site, however ...

Django auto_now and auto_now_add

For Django 1.1. I have this in my models.py: class User(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) When updating a row I ...

Negative custom Django admin FilterSpec

I m working on a custom Django Admin FilterSpec (covered already on SO #991926). My FilterSpec is a replacement for the default filter on ForeignKey(User), and basically replaces the list of all ...

热门标签