繁体   English   中英

如何仅使用 django-mptt 列出父类别?

[英]How can i list Parent categories only with django-mptt?

我只想在一个页面中列出父类别,在另一个页面中列出其他子类别。

模型.py

class Category(MPTTModel):
    name=models.CharField(max_length=50)
    slug=models.SlugField(max_length=50, unique=True, help_text='Uniue Value product page url, created from name.')
    is_active=models.BooleanField(default=True)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at=models.DateTimeField(auto_now=True)
    parent=TreeForeignKey('self',on_delete=models.CASCADE,null=True,blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by=['name']

视图.py

def index(request):
    listing=Category.objects.all()

    context={
        'listing':listing
    }
    return render(request,'catalog/index.html',context)
def index(request):
    parents = Category.objects.filter(parent=None)

    context={
        'parents': parents,
    }
    return render(request,'catalog/index.html',context)

子类别使用:

    Category.objects.exclude(parent=None)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM