From 23964a7b9a57892c3916ddf6722f74a76072f6e3 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 11 Aug 2006 05:20:31 +0000 Subject: [PATCH] Fixed #2458 -- DB API now properly escapes backslashes, so you don't have to double-escape them. Thanks, tom@eggdrop.ch git-svn-id: http://code.djangoproject.com/svn/django/trunk@3552 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/db/models/fields/__init__.py | 2 +- tests/modeltests/lookup/models.py | 13 +++++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 60d684bc42..2067a5984d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -125,6 +125,7 @@ answer newbie questions, and generally made Django that much better: Ivan Sagalaev (Maniac) David Schein sopel + Thomas Steinacher Radek Švarz Swaroop C H Aaron Swartz diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 690695c75d..49eb594838 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -20,7 +20,7 @@ BLANK_CHOICE_DASH = [("", "---------")] 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
    class for a given radio_admin value get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '') diff --git a/tests/modeltests/lookup/models.py b/tests/modeltests/lookup/models.py index a2c0a14158..55bb373a4b 100644 --- a/tests/modeltests/lookup/models.py +++ b/tests/modeltests/lookup/models.py @@ -15,7 +15,7 @@ class Article(models.Model): def __str__(self): return self.headline -API_TESTS = """ +API_TESTS = r""" # Create a couple of Articles. >>> from datetime import datetime >>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26)) @@ -161,13 +161,14 @@ DoesNotExist: Article matching query does not exist. # Underscores and percent signs have special meaning in the underlying -# database library, but Django handles the quoting of them automatically. +# SQL code, but Django handles the quoting of them automatically. >>> a8 = Article(headline='Article_ with underscore', pub_date=datetime(2005, 11, 20)) >>> a8.save() >>> Article.objects.filter(headline__startswith='Article') [, , , , , , , ] >>> Article.objects.filter(headline__startswith='Article_') [] + >>> a9 = Article(headline='Article% with percent sign', pub_date=datetime(2005, 11, 21)) >>> a9.save() >>> Article.objects.filter(headline__startswith='Article') @@ -182,4 +183,12 @@ DoesNotExist: Article matching query does not exist. [, , , , , , , ] >>> Article.objects.exclude(headline="Article 7") [, , , , , , , ] + +# Backslashes also have special meaning in the underlying SQL code, but Django +# automatically quotes them appropriately. +>>> a10 = Article(headline='Article with \\ backslash', pub_date=datetime(2005, 11, 22)) +>>> a10.save() +>>> Article.objects.filter(headline__contains='\\') +[] + """