How to use MPTT for django to get all children?

白昼怎懂夜的黑 提交于 2019-12-11 12:11:42

问题


I'm trying to get all the children of a category:

def list_sub(self, category_name):
   # this will return the parent if exists
   category = Category.objects.filter(seo_title__exact = seo_title).filter(lang__exact = 'pt-PT').filter(level__exact = 1)

   if category:
      # but this doesn't work and in the documentation there are no examples
      # of how to get it. See link about the method
      sub_categories = category.get_children()

http://django-mptt.github.com/django-mptt/models.html#get-children

Update1:

qc = Category.objects.filter(seo_title__exact = cat).filter(lang__exact = 'pt-PT').filter(level__exact = 1)
category = qc.get()

if category:
    qsc = category.get_children()
    sub_categories = qsc.get()

now I get this error: "get() returned more than one Category -- it returned 7! Lookup parameters were {}"

thanks


回答1:


Your problem is not with MPTT. The issue is that category is a queryset, not an instance - get_children() is an model method, not a queryset method.

Use get instead of filter.



来源:https://stackoverflow.com/questions/8048452/how-to-use-mptt-for-django-to-get-all-children

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!