diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2005-11-10 05:36:41 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2005-11-10 05:36:41 +0000 |
| commit | e3e37ed1202f58e4d5e172ecd15af5ab8eda3492 (patch) | |
| tree | 9c57b12cb260c903f7d2142be06e20a9950b82dc /django | |
| parent | 28bce49e59a4dac2ef0fdb14af4ff5b09c5efc95 (diff) | |
Fixed #724 -- Ensured get_next_by_FOO() and get_previous_by_FOO() methods don't skip or duplicate any records in the case of duplicate values. Thanks for reporting the bug, mattycakes@gmail.com
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1155 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/meta/__init__.py | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index 7fe23a4884..22ce51d0a5 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -604,8 +604,8 @@ class ModelBase(type): # for all DateFields and DateTimeFields that cannot be null. # EXAMPLES: Poll.get_next_by_pub_date(), Poll.get_previous_by_pub_date() if not f.null: - setattr(new_class, 'get_next_by_%s' % f.name, curry(method_get_next_or_previous, new_mod.get_object, f, True)) - setattr(new_class, 'get_previous_by_%s' % f.name, curry(method_get_next_or_previous, new_mod.get_object, f, False)) + setattr(new_class, 'get_next_by_%s' % f.name, curry(method_get_next_or_previous, new_mod.get_object, opts, f, True)) + setattr(new_class, 'get_previous_by_%s' % f.name, curry(method_get_next_or_previous, new_mod.get_object, opts, f, False)) # Add "get_thingie_list" for all DateFields and DateTimeFields. # EXAMPLE: polls.get_pub_date_list() func = curry(function_get_date_list, opts, f) @@ -990,10 +990,13 @@ def method_get_order(ordered_obj, self): # DATE-RELATED METHODS ##################### -def method_get_next_or_previous(get_object_func, field, is_next, self, **kwargs): - kwargs.setdefault('where', []).append('%s %s %%s' % (field.column, (is_next and '>' or '<'))) - kwargs.setdefault('params', []).append(str(getattr(self, field.attname))) - kwargs['order_by'] = [(not is_next and '-' or '') + field.name] +def method_get_next_or_previous(get_object_func, opts, field, is_next, self, **kwargs): + op = is_next and '>' or '<' + kwargs.setdefault('where', []).append('(%s %s %%s OR (%s = %%s AND %s %s %%s))' % \ + (field.column, op, field.column, opts.pk.column, op)) + param = str(getattr(self, field.attname)) + kwargs.setdefault('params', []).extend([param, param, getattr(self, opts.pk.attname)]) + kwargs['order_by'] = [(not is_next and '-' or '') + field.name, (not is_next and '-' or '') + opts.pk.name] kwargs['limit'] = 1 return get_object_func(**kwargs) |
