Ignore apostrophes using django-postgresql

229 views
Skip to first unread message

Ada Pineda

unread,
Jul 20, 2012, 7:02:07 PM7/20/12
to django...@googlegroups.com
Hello

I'm asking for your help because I need to do a query on a table that has a field that contains apostrophes, but I need to ignore them (apostrophes) when I'm doing the query. I have to save the data as it is, so I can't just take away those apostrophes before saving to the database. I mean, if the database has "rock'n burger" and a user types "rockn burger", I need to get the value with apostrophe from the database. 

Can anyone help me?

Thanks a lot!

Ian Clelland

unread,
Jul 20, 2012, 7:16:04 PM7/20/12
to django...@googlegroups.com
The queryset's extra() method can help you with that:

    MyModel.objects.extra(where=["replace(db_column_name, '''', '') = %s"], params=["rockn burger"])

(Be careful if you're typing that in manually -- there are lots of single-quotes in it)

Ian

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/lxGzVjrwQgMJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Regards,
Ian Clelland
<clel...@gmail.com>

Russell Keith-Magee

unread,
Jul 20, 2012, 9:22:05 PM7/20/12
to django...@googlegroups.com
To clarify - is your problem removing apostrophes, or having a search
mechanism that can accommodate variations in punctuation?

If the answer is the former, then the answer Ian gave is on the right track.

However, if your actual problem is full-text search, then there are
some better options. If you set up a full text search index, you'll be
able to allow for punctuation, spelling errors, word stems (e.g., a
search for "teacher" finding "teachers") and much more.

django-haystack is the place to start looking; Haystack is a Django
wrapper around a bunch of search backends (Solr, ElasticSearch,
Whoosh, and Xapian). If this is your first look into search backends,
I'd suggest using Elastic Search -- it's really easy to get going, and
is pretty powerful too. Of course, if you're planning to put this into
production, you'd be well served to take a look at the other options,
too.

Yours,
Russ Magee %-)
Reply all
Reply to author
Forward
0 new messages