Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django mptt,包含以树模型作为foreignkey的不同模型实例的计数_Django_Django Mptt - Fatal编程技术网

Django mptt,包含以树模型作为foreignkey的不同模型实例的计数

Django mptt,包含以树模型作为foreignkey的不同模型实例的计数,django,django-mptt,Django,Django Mptt,在网上商店中,我使用django-mptt对多个级别的产品类别进行分类。我也有每个属于一个类别的产品 现在,我想将类别树可视化如下: all(18) - edible (10) -- food (4) -- drink (6) - inedible (8) -- wood (3) -- rock (5) 其中,范围中的数字是每个类别的产品计数 我能够可视化分类树。我也可以将产品数量放在类别名称后面,但我这样做似乎效率很低。我基本上在category模型中有一个get\u number\u of

在网上商店中,我使用
django-mptt
对多个级别的产品类别进行分类。我也有每个属于一个类别的产品

现在,我想将类别树可视化如下:

all(18)
- edible (10)
-- food (4)
-- drink (6)
- inedible (8)
-- wood (3)
-- rock (5)
其中,范围中的数字是每个类别的产品计数

我能够可视化分类树。我也可以将产品数量放在类别名称后面,但我这样做似乎效率很低。我基本上在category模型中有一个
get\u number\u of_products()
方法,它返回一个类别的产品数量。但是每次都需要一个新的数据库查询


解决这一问题的一种方法是使用缓存,因为我不经常更改产品计数,但我更喜欢一种需要较少数据库查询的方法来获取产品计数树。

我的答案与Django Oscar有关,但您可能会发现它很有帮助

{{for tree category in tree_categories}}
是一个category类,因此可以通过

{{ tree_category.get_num_children }}
你想要的是

例如,在admin中,您可以使用以下内容:

class CategoryAdmin(DraggableMPTTAdmin):
    mptt_indent_field = "name"
    list_display = ('tree_actions', 'indented_title', 
                    'related_products_count', 'related_products_cumulative_count')
    list_display_links = ('indented_title',)

    def get_queryset(self, request):
        qs = super().get_queryset(request)

        # Add cumulative product count
        qs = Category.objects.add_related_count(
                qs,
                Product,
                'category',
                'products_cumulative_count',
                cumulative=True)

        # Add non cumulative recipe count
        qs = Category.objects.add_related_count(qs,
                 Product,
                 'categories',
                 'products_count',
                 cumulative=False)
        return qs

    def related_products_count(self, instance):
        return instance.products_count
    related_product_count.short_description = 'Related products (for this specific category)'

    def related_products_cumulative_count(self, instance):
        return instance.products_cumulative_count
    related_products_cumulative_count.short_description = 'Related products (in tree)'

这或
get\u substant\u count
返回当前实例所属的类别数,但不返回链接到该类别的实例数。我还在文档中添加了此示例:
class CategoryAdmin(DraggableMPTTAdmin):
    mptt_indent_field = "name"
    list_display = ('tree_actions', 'indented_title', 
                    'related_products_count', 'related_products_cumulative_count')
    list_display_links = ('indented_title',)

    def get_queryset(self, request):
        qs = super().get_queryset(request)

        # Add cumulative product count
        qs = Category.objects.add_related_count(
                qs,
                Product,
                'category',
                'products_cumulative_count',
                cumulative=True)

        # Add non cumulative recipe count
        qs = Category.objects.add_related_count(qs,
                 Product,
                 'categories',
                 'products_count',
                 cumulative=False)
        return qs

    def related_products_count(self, instance):
        return instance.products_count
    related_product_count.short_description = 'Related products (for this specific category)'

    def related_products_cumulative_count(self, instance):
        return instance.products_cumulative_count
    related_products_cumulative_count.short_description = 'Related products (in tree)'