本文介绍了如何使用MPTT for django来获取所有的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def list_sub(self,Category_name):
#这将返回父,如果存在
category = Category.objects.filter(seo_title__exact = seo_title).filter(lang__exact ='pt-PT')。filter(level__exact = 1)

如果类别:
#但这不工作,在文档中没有例子
#如何得到它。查看关于方法的链接
sub_categories = category.get_children()



Update1:​​



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

如果类别:
qsc = category.get_children()
sub_categories = qsc.get()

现在我得到这个错误:get()返回多个类别 - 它返回7!查找参数是{}



谢谢

解决方案

您的问题不在于MPTT。问题是类别是一个查询集,而不是一个实例 - get_children()是一种模型方法,而不是一个查询方法。



使用获取而不是过滤器 / p>

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

解决方案

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.

这篇关于如何使用MPTT for django来获取所有的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 22:28