diff --git a/django/db/models/base.py b/django/db/models/base.py
index 9cf0834..8338a18 100644
a
|
b
|
class Model(object):
|
590 | 590 | return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True) |
591 | 591 | |
592 | 592 | def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs): |
593 | | if not self.pk: |
| 593 | if self._state.adding: |
594 | 594 | raise ValueError("get_next/get_previous cannot be used on unsaved objects.") |
595 | 595 | op = is_next and 'gt' or 'lt' |
596 | 596 | order = not is_next and '-' or '' |
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index cedf308..de0c1a7 100644
a
|
b
|
class SingleRelatedObjectDescriptor(object):
|
235 | 235 | try: |
236 | 236 | return getattr(instance, self.cache_name) |
237 | 237 | except AttributeError: |
| 238 | if instance._state.adding: |
| 239 | raise ValueError("Cannot retrieve related objects until the instance has been saved") |
| 240 | |
238 | 241 | params = {'%s__pk' % self.related.field.name: instance._get_pk_val()} |
239 | 242 | db = router.db_for_read(self.related.model, instance=instance) |
240 | 243 | rel_obj = self.related.model._base_manager.using(db).get(**params) |
… |
… |
class ForeignRelatedObjectsDescriptor(object):
|
464 | 467 | |
465 | 468 | manager = RelatedManager() |
466 | 469 | attname = rel_field.rel.get_related_field().name |
| 470 | |
| 471 | if instance._state.adding: |
| 472 | raise ValueError("Cannot retrieve related objects until the instance has been saved") |
467 | 473 | manager.core_filters = {'%s__%s' % (rel_field.name, attname): |
468 | 474 | getattr(instance, attname)} |
469 | 475 | manager.model = self.related.model |
… |
… |
def create_many_related_manager(superclass, rel=False):
|
488 | 494 | self.through = through |
489 | 495 | self._pk_val = self.instance.pk |
490 | 496 | self.reverse = reverse |
491 | | if self._pk_val is None: |
492 | | raise ValueError("%r instance needs to have a primary key value before a many-to-many relationship can be used." % instance.__class__.__name__) |
| 497 | if self.instance._state.adding: |
| 498 | raise ValueError("%r instance needs to be saved before a many-to-many relationship can be used." % instance.__class__.__name__) |
493 | 499 | |
494 | 500 | def get_query_set(self): |
495 | 501 | db = self._db or router.db_for_read(self.instance.__class__, instance=self.instance) |
diff --git a/tests/modeltests/lookup/tests.py b/tests/modeltests/lookup/tests.py
index 3f40bf1..2f59202 100644
a
|
b
|
class LookupTests(TestCase):
|
351 | 351 | '<Article: Article 2>') |
352 | 352 | self.assertEqual(repr(self.a2.get_previous_by_pub_date()), |
353 | 353 | '<Article: Article 1>') |
| 354 | self.assertRaises(ValueError, lambda: Article().get_previous_by_pub_date()) |
354 | 355 | |
355 | 356 | def test_escaping(self): |
356 | 357 | # Underscores, percent signs and backslashes have special meaning in the |
diff --git a/tests/modeltests/many_to_one/tests.py b/tests/modeltests/many_to_one/tests.py
index 8a2af21..1486ddc 100644
a
|
b
|
class ManyToOneTests(TestCase):
|
372 | 372 | # recursive don't cause recursion depth problems under deepcopy. |
373 | 373 | self.r.cached_query = Article.objects.filter(reporter=self.r) |
374 | 374 | self.assertEqual(repr(deepcopy(self.r)), "<Reporter: John Smith>") |
| 375 | |
| 376 | def test_related_before_save(self): |
| 377 | # You cannot get the related objects before the main object is saved. |
| 378 | # See ticket #14615 |
| 379 | self.assertRaises(ValueError, getattr, Reporter(), 'article_set') |
diff --git a/tests/modeltests/one_to_one/tests.py b/tests/modeltests/one_to_one/tests.py
index c3e1704..31e1bc2 100644
a
|
b
|
class OneToOneTests(TestCase):
|
117 | 117 | mm = MultiModel(link1=self.p2, link2=o1, name="x1") |
118 | 118 | self.assertRaises(IntegrityError, mm.save) |
119 | 119 | transaction.savepoint_rollback(sid) |
| 120 | |
| 121 | def test_related_before_save(self): |
| 122 | # You cannot get the related objects before the main object is saved. |
| 123 | # See ticket #14615 |
| 124 | self.assertRaises(ValueError, getattr, Place(), 'restaurant') |
diff --git a/tests/regressiontests/null_queries/tests.py b/tests/regressiontests/null_queries/tests.py
index ab66ff6..1cb6232 100644
a
|
b
|
class NullQueriesTests(TestCase):
|
42 | 42 | # Can't use None on anything other than __exact |
43 | 43 | self.assertRaises(ValueError, Choice.objects.filter, foo__gt=None) |
44 | 44 | |
45 | | # Related managers use __exact=None implicitly if the object hasn't been saved. |
46 | | p2 = Poll(question="How?") |
47 | | self.assertEqual(repr(p2.choice_set.all()), '[]') |
48 | | |
49 | 45 | def test_reverse_relations(self): |
50 | 46 | """ |
51 | 47 | Querying across reverse relations and then another relation should |