繁体   English   中英

通过组合多个 Django 滤波器获得独特的输出

[英]Get unique outputs from combining more than one Django filters

因此,我正在尝试在我的网站上创建一个搜索引擎,用户可以在其中输入查询,然后在产品namedescription中存在查询的地方显示这些结果。 我的产品 model 如下:-

'''Product model to store the details of all the products'''
class Product(models.Model):

    # Define the fields of the product model
    name = models.CharField(max_length=100)
    price = models.IntegerField(default=0)
    quantity = models.IntegerField(default=0)
    description = models.CharField(max_length=200, default='', null=True, blank=True)
    image = models.ImageField(upload_to='uploads/images/products')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, default=1)    # Foriegn key with Category Model
    store = models.ForeignKey(Store, on_delete=models.CASCADE, default=1)

现在,我创建了一个搜索过滤器,在其中按名称过滤,然后按描述过滤,然后组合结果以返回查询集。 问题是有时可以在名称和描述中找到查询,所以我得到这样的重复条目

在此处输入图像描述

下面提到了我在 ProductView 中的过滤代码:-

class ProductView(generics.ListAPIView):
    ''' Product View to return the details of all the products and filter by ID or string '''

    # Define class variables 
    queryset = []

    # Manage a get request
    def get(self, request):
        
        ''' Display all the products in our database if no id is passed 
            and if a store ID and search is passed then use those parameters'''
        
        # Get all the parameters sent in the data 
        store_id = request.GET.get('store_id')
        search_query = request.GET.get('search')
        category = request.GET.get('category')

        print(store_id, search_query, category)
        queryset = Product.get_all_products()
        
        # Apply filters to the data
        if store_id:
            queryset = queryset.filter(store=store_id)
        
        if category:
            queryset = queryset.filter(category=category)
        
        if search_query: 
            

            # THIS IS WHERE THE SEARCH FILTERING SHOULD HAPPEN

            names = queryset.filter(name__icontains=search_query)
            descriptions = queryset.filter(description__icontains=search_query)

            queryset = list(chain(names, descriptions))
        
        return Response(ProductSerializer(queryset, many = True).data)
        

我希望只能得到一个结果,因为它是相同的产品。 有没有办法只能将不同的结果保存在查询集中。 我做了一个循环来检查descriptions中的结果是否该项目已经存在于名称中,但效果不是很好。 我怎样才能解决这个问题?

你有两个问题。

首先,您永远不应该像这样评估和合并两个查询集结果。

如果您想 select 所有具有包含您的icontaining的名称或描述的search_query ,请使用Q object。

要仅选择查询集中的唯一对象,请使用distinct

所以你可以把最后几行改成这样。

        if search_query:
            queryset = queryset.filter(
                Q(name__icontains=search_query) | Q(description__icontains=search_query)
            )
        
        queryset = queryset.distinct()  # probably won't be needed anymore

我添加了一个 forloop 来完成这项工作,但如果有人有更好的解决方案,请将其留在评论中。 这是我写的最后一个视图。

class ProductView(generics.ListAPIView):
    ''' Product View to return the details of all the products and filter by ID or string '''

    # Define class variables 
    serializer_class = ProductSerializer
    queryset = Product.objects.all()

    search_fields = ['name','description']
    filter_backends = (SearchFilter,)
    queryset = []

    # Manage a get request
    def get(self, request):
        
        ''' Display all the products in our database if no id is passed 
            and if a store ID and search is passed then use those parameters'''
        
        # Get all the parameters sent in the data 
        store_id = request.GET.get('store_id', None)
        search_query = request.GET.get('search')
        category = request.GET.get('category')

        print(store_id, search_query, category)
        queryset = Product.get_all_products()
        
        # Apply filters to the data
        if store_id:
            queryset = queryset.filter(store=store_id)
        
        if category:
            queryset = queryset.filter(category=category)
        
        if search_query: 
            pass 
            names = queryset.filter(name__icontains=search_query)
            descriptions = queryset.filter(description__icontains=search_query)

            # Iterate through the names and get all ids 
            ids = []
            for i in range(len(names)):
                ids.append(names[i].id)
                print(type(names[i].id), print(type(names[i])))
            
            # iterate through descriptions 
            for i in range(len(descriptions)): 
                if descriptions[i].id not in ids:
                    ids.append(descriptions[i].id)
            
            queryset = Product.objects.filter(id__in=ids)
        
        return Response(ProductSerializer(queryset, many = True).data)

暂无
暂无

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

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