From 9802a73e254de90230f8c3574ec46e6761691ba7 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 11 Sep 2010 00:20:35 +0000 Subject: [PATCH] Better error message for calling get_next_by_* on unsaved models. Patch from Marc Fargas. Fixed #7435. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13738 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 2 ++ docs/ref/models/instances.txt | 2 ++ 2 files changed, 4 insertions(+) diff --git a/django/db/models/base.py b/django/db/models/base.py index 1927b1eb2a..b3deda13d4 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -644,6 +644,8 @@ class Model(object): return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True) def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs): + if not self.pk: + raise ValueError("get_next/get_previous cannot be used on unsaved objects.") op = is_next and 'gt' or 'lt' order = not is_next and '-' or '' param = smart_str(getattr(self, field.attname)) diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index 8bb6cf9cc5..dfe4c747e8 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -568,3 +568,5 @@ described in :ref:`Field lookups `. Note that in the case of identical date values, these methods will use the ID as a fallback check. This guarantees that no records are skipped or duplicated. + +That also means you cannot use those methods on unsaved objects.