Word count query in Django -


given model both boolean , textfield fields, want query finds records match criteria , have more "n" words in textfield. possible? e..g.:

class item(models.model):      ...         notes = models.textfield(blank=true,)     has_media = models.booleanfield(default=false)     completed = models.booleanfield(default=false)     ... 

this easy:

items = item.objects.filter(completed=true,has_media=true) 

but how can filter subset of records "notes" field has more than, say, 25 words?

try this:

item.objects.extra(where=["length(notes) - length(replace(notes, ' ', ''))+1 > %s"], params=[25]) 

this code uses django's extra queryset method add custom where clause. calculation in where clause counts occurances of "space" character, assuming words prefixed 1 space character. adding 1 result accounts first word.

of course, calculation approximation real word count, if has precise, i'd word count in python.


Comments

Popular posts from this blog

SAP Web Service from .NET via WCF -

Optimized Line drawing in QT -

datetime - str to time in python -