在Django - Oscar中的视图中过滤对象? [英] Filtering Objects in a View in Django - Oscar?

查看:131
本文介绍了在Django - Oscar中的视图中过滤对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 ProductCategoryView 。它应该相当简单的实现,但是花费太多时间来理解。我有第二个想法向奥斯卡提出,因为似乎很难扩展。



ProductCategoryView 返回某个类别的产品。我想根据产品型号中的某些字段对其进行排序,说价格。首先,我将父类通用类从 TemplateView 更改为 ListView ,以便我可以使用 get_queryset 方法。然后我覆盖 get_queryset 方法如下,并在其中编写一个简单的查询。仍然排序不会发生,虽然流程进入 get_queryset 方法。

  def get_queryset(self):
queryset = Product.objects.filter(categories = self.category)
logger.debug(inside)
queryset = queryset.order_by(price)
return queryset

那么什么方法我必须覆盖每次编辑奥斯卡都会有这么多的麻烦吗?



P.S:我刚刚问过很多有关Django / Oscar Class的视图。所以我可能看起来像一个帮助吸血鬼。如果是这样,请忽略这个问题。

解决方案

@Anentropic是对的,但让我详细说一下。

Oscar基于搜索引擎的所有浏览视图,因此可以用于缩小返回的产品列表。默认情况下, Solr Elasticsearch



如果不使用Solr或Elasticsearch,默认的实现是 SimpleProductSearchHandler 。它不使用任何外部服务,而是调用 Product.browsable.get_queryset() 。该代码位于 oscar.apps.catalogue.managers ,并可以定制,以提供定制订单。



这是假设您不想使用搜索引擎,因为需要更改其他搜索处理程序类的结果顺序所需的自定义是后端特定的。


Trying to implement a simple ordering query on ProductCategoryView in Django-Oscar. It should be fairly simple to implement but it's taking too much time to understand. I'm having second thoughts to go forward with Oscar or not as it seems difficult to extend.

The ProductCategoryView returns products of a certain category. I want to sort them according to certain field in product model say price. First I change the parent generic class from TemplateView to ListView so that I can use get_queryset method. Then I override the get_queryset method as below and write a simple queryset in that. Still the sorting doesn't happen though the flow does go inside the get_queryset method.

def get_queryset(self):
        queryset = Product.objects.filter(categories = self.category)
        logger.debug("inside")
        queryset = queryset.order_by("price")           
        return queryset

So what methods I have to overwrite. Will there be so much trouble editing Oscar every time or I'm missing something?

P.S : I have asked lot of questions around Django/Oscar Class based view recently . So I might look like a Help Vampire. Please ignore this question if that is the case.

解决方案

@Anentropic is right but let me elaborate a bit.

Oscar bases all its browse views on search engines so faceting can be used to narrow down the list of products returned. By default there are search engine integrations for Solr and Elasticsearch.

If you don't use Solr or Elasticsearch, the default implementation is SimpleProductSearchHandler. It does not use any external services but instead calls Product.browsable.get_queryset(). That code lives in oscar.apps.catalogue.managers and could be customized to provide custom ordering.

This is all assuming you don't want to use a search engine as customizations required to change the order of results for other search handler classes are backend-specific.

这篇关于在Django - Oscar中的视图中过滤对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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