Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 9802a73e254de90230f8c3574ec46e6761691ba7 1 parent 506d559
Malcolm Tredinnick authored September 11, 2010
2  django/db/models/base.py
@@ -644,6 +644,8 @@ def _get_FIELD_display(self, field):
644 644
         return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True)
645 645
 
646 646
     def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
  647
+        if not self.pk:
  648
+            raise ValueError("get_next/get_previous cannot be used on unsaved objects.")
647 649
         op = is_next and 'gt' or 'lt'
648 650
         order = not is_next and '-' or ''
649 651
         param = smart_str(getattr(self, field.attname))
2  docs/ref/models/instances.txt
@@ -568,3 +568,5 @@ described in :ref:`Field lookups <field-lookups>`.
568 568
 
569 569
 Note that in the case of identical date values, these methods will use the ID
570 570
 as a fallback check. This guarantees that no records are skipped or duplicated.
  571
+
  572
+That also means you cannot use those methods on unsaved objects.

0 notes on commit 9802a73

Please sign in to comment.
Something went wrong with that request. Please try again.