[Answer]-How to custom sort a Django queryset

1πŸ‘

@bebraw in the comments is actually right.

Creating a special sort field is how it is done. This is custom for search frameworks like SOLR, as well. You even might apply special text analysis to sort according to language.

In your case, if it’s just as simple as to remove the stop words (a short list of articles), you would create a regular model field that copies the data from the other field and removes the stop words.

The benefit from using a regular DB field: you can create a DB index (e.g. on UPPER(value)) and be able to sort case insensitive supported by the DB index (you will have to add an extra field with QuerySet.extra to sort on UPPER(value)).

This will allow fast sorted and paged results. If you do everything in Django you will have to retrieve the whole data. This might be fast enough for some hundreds of rows but will not scale at all if the data increases.

HOWEVER:
Be careful with stop words depending on your data. The anti example to stop words is the title β€œTo Be or Not To Be” which consists completely of what you might easily classify as stop words and would simply be annihilated when running through such a filter.

πŸ‘€Risadinha

Leave a comment