From a8a133cc6cf4c3a5a2deb9d8989ba09b09ace736 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Tue, 20 Jun 2006 02:17:14 +0000 Subject: [PATCH] Fixed #1857 -- Fixed get_previous_by_FIELD and get_next_by_FIELD to work properly with keyword arguments. Patch from tom@jerakeen.org. git-svn-id: http://code.djangoproject.com/svn/django/trunk@3157 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 4 ++-- tests/modeltests/lookup/models.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index 7242e6baa7..864363d9ee 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -271,13 +271,13 @@ class Model(object): value = getattr(self, field.attname) return dict(field.choices).get(value, value) - def _get_next_or_previous_by_FIELD(self, field, is_next): + def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs): op = is_next and '>' or '<' where = '(%s %s %%s OR (%s = %%s AND %s.%s %s %%s))' % \ (backend.quote_name(field.column), op, backend.quote_name(field.column), backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column), op) param = str(getattr(self, field.attname)) - q = self.__class__._default_manager.order_by((not is_next and '-' or '') + field.name, (not is_next and '-' or '') + self._meta.pk.name) + q = self.__class__._default_manager.filter(**kwargs).order_by((not is_next and '-' or '') + field.name, (not is_next and '-' or '') + self._meta.pk.name) q._where.append(where) q._params.extend([param, param, getattr(self, self._meta.pk.attname)]) try: diff --git a/tests/modeltests/lookup/models.py b/tests/modeltests/lookup/models.py index 9ac53b1677..fc12c99f60 100644 --- a/tests/modeltests/lookup/models.py +++ b/tests/modeltests/lookup/models.py @@ -128,6 +128,8 @@ True >>> a2.get_next_by_pub_date() +>>> a2.get_next_by_pub_date( headline__endswith = '6' ) + >>> a3.get_next_by_pub_date() >>> a4.get_next_by_pub_date()