From 57970663831908e8f1e8bb787b04ac923d123e41 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 19 Jul 2005 15:24:03 +0000 Subject: [PATCH] Added support for istartswith and iendswith in database API git-svn-id: http://code.djangoproject.com/svn/django/trunk@207 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/db/backends/mysql.py | 4 ++- django/core/db/backends/postgresql.py | 4 ++- docs/db-api.txt | 36 ++++++++++++++++----------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py index 817be0c19a..9e96ed692e 100644 --- a/django/core/db/backends/mysql.py +++ b/django/core/db/backends/mysql.py @@ -99,7 +99,9 @@ OPERATOR_MAPPING = { 'lt': '<', 'lte': '<=', 'startswith': 'LIKE', - 'endswith': 'LIKE' + 'endswith': 'LIKE', + 'istartswith': 'ILIKE', + 'iendswith': 'ILIKE', } # This dictionary maps Field objects to their associated MySQL column diff --git a/django/core/db/backends/postgresql.py b/django/core/db/backends/postgresql.py index 8a629453b3..8f82e1b23f 100644 --- a/django/core/db/backends/postgresql.py +++ b/django/core/db/backends/postgresql.py @@ -93,7 +93,9 @@ OPERATOR_MAPPING = { 'lt': '<', 'lte': '<=', 'startswith': 'LIKE', - 'endswith': 'LIKE' + 'endswith': 'LIKE', + 'istartswith': 'ILIKE', + 'iendswith': 'ILIKE', } # This dictionary maps Field objects to their associated PostgreSQL column diff --git a/docs/db-api.txt b/docs/db-api.txt index 54a704d3f0..7d51e8e348 100644 --- a/docs/db-api.txt +++ b/docs/db-api.txt @@ -52,21 +52,29 @@ The DB API supports the following lookup types: ========== ============================================================== Type Description ========== ============================================================== - exact Exact match: ``polls.get_object(id__exact=14)`` + exact Exact match: ``polls.get_object(id__exact=14)``. iexact Case-insensitive exact match: ``polls.get_list(slug__iexact="foo")`` matches a slug of ``foo``, ``FOO``, ``fOo``, etc. contains Case-sensitive containment test: ``polls.get_list(question__contains="spam")`` returns all polls - that contain "spam" in the question. - icontains Case-insensitive containment test - gt Greater than: ``polls.get_list(id__gt=4)`` - gte Greater than or equal to - lt Less than - lte Less than or equal to + that contain "spam" in the question. (PostgreSQL only. MySQL + doesn't support case-sensitive LIKE statements; ``contains`` + will act like ``icontains`` for MySQL.) + icontains Case-insensitive containment test. + gt Greater than: ``polls.get_list(id__gt=4)``. + gte Greater than or equal to. + lt Less than. + lte Less than or equal to. startswith Case-sensitive starts-with: - ``polls.get_list(question_startswith="Would")`` - endswith Case-sensitive ends-with + ``polls.get_list(question_startswith="Would")``. (PostgreSQL + only. MySQL doesn't support case-sensitive LIKE statements; + ``startswith`` will act like ``istartswith`` for MySQL.) + endswith Case-sensitive ends-with. (PostgreSQL only. MySQL doesn't + support case-sensitive LIKE statements; ``endswith`` will act + like ``iendswith`` for MySQL.) + istartswith Case-insensitive starts-with. + iendswith Case-insensitive ends-with. range Range test: ``polls.get_list(pub_date__range=(start_date, end_date))`` returns all polls with a pub_date between ``start_date`` @@ -308,13 +316,13 @@ Creating new objects Creating new objects (i.e. ``INSERT``) is done by creating new instances of objects then calling save() on them:: - >>> p = polls.Poll(id=None, + >>> p = polls.Poll(id=None, ... slug="eggs", ... question="How do you like your eggs?", ... pub_date=datetime.datetime.now(), ... expire_date=some_future_date) >>> p.save() - + Calling ``save()`` on an object with an id if ``None`` signifies to Django that the object is new and should be inserted. @@ -326,7 +334,7 @@ Related objects (i.e. ``Choices``) are created using convience functions:: >>> p.add_choice(choice="Poached", votes=0) >>> p.get_choice_count() 4 - + Each of those ``add_choice`` methods is equivilent to (except obviously much simpler than):: @@ -335,9 +343,9 @@ simpler than):: ... choice="Over easy", ... votes=0) >>> c.save() - + Note that when using the `add_foo()`` methods, you do not give any value -for the ``id`` field, nor do you give a value for the field that stores +for the ``id`` field, nor do you give a value for the field that stores the relation (``poll_id`` in this case). Deleting objects