[Answer]-Custom manager object not working in Django?

1👍

This won’t work because of how you constructed query. Let’s analyze your query:

filter(Q(body_text__in=wordlist) | Q(title__in=wordlist))

It seems you want to search for keywords in content and titles. But body_text__in=wordlist means that filter will be satisfied if your whole body of text will be either ‘hello’ or ‘world’ or ‘clean.’ My assumption is that it’s not what you want. Instead what you are looking for is iterating over keywords and using __contains condition. I haven’t written Django queries in quite a while, so I will write some ugly code that maybe will present the general idea:

full_query = null
for keyword in wordlist:
  if full_query is null:
    full_quey = Q(body_text__contains=keyword) | Q(title__in=keywords)
  else:
    full_query = full_query | (Q(body_text__contains=keyword) | Q(title__in=keywords))
posts=super(PostingFilterManager,self).get_query_set().filter
        (Q(body_text__in=wordlist) | Q(title__in=wordlist))

Also, additional suggestion. What you are doing here is full text search and what you are doing is probably not the best way of doing it. Probably a better way would be to construct an index and search an index.

Consider reading Wikipedia article about full text search, especially indexing: http://en.wikipedia.org/wiki/Full_text_search

Leave a comment