Filtering Objects in a View in Django - Oscar?

2020-07-30 02:54发布

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.

1条回答
神经病院院长
2楼-- · 2020-07-30 03:17

@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.

查看更多
登录 后发表回答