diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-08-11 05:20:31 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-08-11 05:20:31 +0000 |
| commit | 23964a7b9a57892c3916ddf6722f74a76072f6e3 (patch) | |
| tree | 1d7c1667fe84a24dddc27b7049ef268843d08301 | |
| parent | 3e22c8ff527cacb37e8a0e6a8864f6f459f74684 (diff) | |
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
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | django/db/models/fields/__init__.py | 2 | ||||
| -rw-r--r-- | tests/modeltests/lookup/models.py | 13 |
3 files changed, 13 insertions, 3 deletions
@@ -125,6 +125,7 @@ answer newbie questions, and generally made Django that much better: Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/> David Schein sopel + Thomas Steinacher <tom@eggdrop.ch> Radek Švarz <http://www.svarz.cz/translate/> Swaroop C H <http://www.swaroopch.info> Aaron Swartz <http://www.aaronsw.com/> 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 <ul> 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. <Article: Article 1> # 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: Article_ with underscore>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 7>, <Article: Article 1>] >>> Article.objects.filter(headline__startswith='Article_') [<Article: Article_ with underscore>] + >>> 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: Article% with percent sign>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 7>, <Article: Article 1>] >>> Article.objects.exclude(headline="Article 7") [<Article: Article% with percent sign>, <Article: Article_ with underscore>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 1>] + +# 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='\\') +[<Article: Article with \ backslash>] + """ |
