7585

Filtering Objects in a View in Django - Oscar?

Question:

Trying to implement a simple ordering query on <a href="http://django-oscar.readthedocs.org/en/releases-1.1/_modules/oscar/apps/catalogue/views.html#ProductCategoryView" rel="nofollow">ProductCategoryView</a> 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 <a href="http://django-oscar.readthedocs.org/en/releases-1.1/_modules/oscar/apps/catalogue/views.html#ProductCategoryView" rel="nofollow">ProductCategoryView</a> 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 <strong>though the flow does go inside the get_queryset method.</strong>

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 <a href="https://meta.stackoverflow.com/questions/258206/what-is-a-help-vampire" rel="nofollow">Help Vampire</a>. Please ignore this question if that is the case.

Answer1:

@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 <a href="http://lucene.apache.org/solr/" rel="nofollow">Solr</a> and <a href="https://www.elastic.co/products/elasticsearch" rel="nofollow">Elasticsearch</a>.

If you don't use Solr or Elasticsearch, the default implementation is SimpleProductSearchHandler. It does not use any external services but instead <a href="https://github.com/django-oscar/django-oscar/blob/releases/1.1/src/oscar/apps/catalogue/search_handlers.py#L101-L105" rel="nofollow">calls Product.browsable.get_queryset()</a>. That code lives in <a href="https://github.com/django-oscar/django-oscar/blob/releases/1.1/src/oscar/apps/catalogue/managers.py#L44-L52" rel="nofollow">oscar.apps.catalogue.managers</a> 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.

Recommend

  • Instance parameter not being passed in nested inlines form
  • Convert django RawQuerySet to Queryset
  • Django aggregation and following foreign key relations
  • Filter a DJango ContentType queryset to only include models that have a specific method
  • Select a valid choice ModelChoiceField
  • Django template filter queryset
  • query.group_by in Django 1.9
  • VBA: Extract Top 'x' Entries from each category
  • Django RESTful API error “type object 'User' has no attribute '_meta'”
  • Fancy dynamic list in Android: TableLayout vs ListView
  • React Native fetch is not a function
  • UWP change ComboBox position when opening
  • SSRS 2008 - Sorting within a group
  • How to select table rows/complete table?
  • Wait for .each() .getJSON request to finish before executing a callback
  • how to read a file in prolog?
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Runtime.exec() gives Error: Could not find or load main class
  • Magento get URL before current
  • WPF version of .ScaleControl?
  • Do query loads all the data in memory
  • quiver not drawing arrows just lots of blue, matlab
  • How to make R's read_csv2() recognise the text characters properly
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Grails calculated field in SQL
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Debug.DrawLine not showing in the GameView
  • MongoDB in PHP using aggregate to group by _id is null not working
  • CSS Linear-gradient formatting issue accross different browsers
  • How to add date and time under each post in guestbook in google app engine
  • JSON with duplicate key names losing information when parsed
  • Trying to switch camera back to front but getting exception
  • Return words with double consecutive letters
  • XCode can't find symbols for a specific iOS library/framework project
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Getting Messege Twice Using IMvxMessenger
  • Programmatically clearing map cache
  • Python/Django TangoWithDjango Models and Databases