具有树模型作为外键的不同模型实例数的mptt [英] mptt with count of a different models instances that have tree model as foreignkey

查看:87
本文介绍了具有树模型作为外键的不同模型实例数的mptt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一家网上商店中,我将django-mptt用于具有多个级别的产品类别.我也有每个属于一个类别的产品.

In an online shop I use django-mptt for product categories with several levels. I also have products that each belong to a category.

现在,我想像这样可视化类别树:

Now I'd like to visualise the category tree like this:

all(18)
- edible (10)
-- food (4)
-- drink (6)
- inedible (8)
-- wood (3)
-- rock (5)

其中范围中的数字是每个类别的产品计数.

where the numbers in scopes are the product counts for each category.

我能够可视化类别树.我也可以将产品计数放在类别名称后面,但是我的操作方式似乎效率很低.我基本上在类别模型上有一个get_number_of_products()方法,该方法返回一个类别的产品数量.但这每次都需要一个新的数据库查询.

I am able to visualize the category tree. I can also place the product count behind the category names, but the way I am doing it seems very inefficient. I basically have a get_number_of_products() methods at the category model which returns the number of products for a category. But this needs a new database query each time.

一种处理此问题的方法是使用缓存,因为我不经常更改产品数量,但是我更喜欢一种方法,该方法需要较少的数据库查询才能获得具有产品数量的树.

One way to deal with this is to use caching as I don't change the product count that often, but I'd prefer a method that needs less database queries to get the tree with the product count.

推荐答案

您想要的是

What you want is add_related_count

Category.objects.add_related_count(
   Category.objects.get_descendants(include_self=True),  # Queryset
   Product,  # Related mobile
   'category',  # Name of the foreignkey field
   'count',  # Name of the property added to the collection
    cumulative=True)  # Cumulative or not.

例如,在管理员中,您可以使用类似的内容:

For instance in the admin you can use something like that:

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)'

这篇关于具有树模型作为外键的不同模型实例数的mptt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆