Django

Code

Ticket #2458 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] Backslashes in LIKE queries are not escaped

Reported by: tom@eggdrop.ch Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Say you have a model called MyModel? with a TextField? called text and want to get all objects where text contains a backslash. The following query, however, doesn't return anything:

models.MyModel.objects.filter(text__conatins='\\')

This query does:

models.MyModel.objects.filter(text__conatins='\\\\')

Here is a patch that fixes the problem:

Index: django/db/models/fields/__init__.py
===================================================================
--- django/db/models/fields/__init__.py (revision 3496)
+++ django/db/models/fields/__init__.py (working copy)
@@ -20,7 +20,7 @@
 BLANK_CHOICE_NONE = [("", "None")]
 
 # prepares a value for use in a LIKE query
-prep_for_like_query = lambda x: str(x).replace("%", "\%").replace("_", "\_")
+prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
 
 # returns the <ul> class for a given radio_admin value
 get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')

Attachments

Change History

07/30/06 11:17:39 changed by anonymous

  • component changed from Admin interface to Database wrapper.

08/02/06 12:00:08 changed by anonymous

  • summary changed from Backslashes in LIKE queries are not escaped to [patch] Backslashes in LIKE queries are not escaped.

08/11/06 00:20:31 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

(In [3552]) Fixed #2458 -- DB API now properly escapes backslashes, so you don't have to double-escape them. Thanks, tom@eggdrop.ch


Add/Change #2458 ([patch] Backslashes in LIKE queries are not escaped)




Change Properties
Action