Code

Ticket #1857: get_next_previous.diff

File get_next_previous.diff, 1.6 KB (added by tom@…, 8 years ago)
Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revision 2901)
4+++ django/db/models/base.py    (working copy)
5@@ -258,13 +258,13 @@
6         value = getattr(self, field.attname)
7         return dict(field.choices).get(value, value)
8 
9-    def _get_next_or_previous_by_FIELD(self, field, is_next):
10+    def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
11         op = is_next and '>' or '<'
12         where = '(%s %s %%s OR (%s = %%s AND %s.%s %s %%s))' % \
13             (backend.quote_name(field.column), op, backend.quote_name(field.column),
14             backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column), op)
15         param = str(getattr(self, field.attname))
16-        q = self.__class__._default_manager.order_by((not is_next and '-' or '') + field.name, (not is_next and '-' or '') + self._meta.pk.name)
17+        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)
18         q._where.append(where)
19         q._params.extend([param, param, getattr(self, self._meta.pk.attname)])
20         try:
21Index: tests/modeltests/lookup/models.py
22===================================================================
23--- tests/modeltests/lookup/models.py   (revision 2901)
24+++ tests/modeltests/lookup/models.py   (working copy)
25@@ -128,6 +128,8 @@
26 Article 2
27 >>> a2.get_next_by_pub_date()
28 Article 3
29+>>> a2.get_next_by_pub_date( headline__endswith = '6' )
30+Article 6
31 >>> a3.get_next_by_pub_date()
32 Article 7
33 >>> a4.get_next_by_pub_date()