diff -r 24c30016c52f django/db/models/base.py
a
|
b
|
|
585 | 585 | return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True) |
586 | 586 | |
587 | 587 | def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs): |
588 | | if not self.pk: |
| 588 | if self._state.adding: |
589 | 589 | raise ValueError("get_next/get_previous cannot be used on unsaved objects.") |
590 | 590 | op = is_next and 'gt' or 'lt' |
591 | 591 | order = not is_next and '-' or '' |
diff -r 24c30016c52f django/db/models/fields/related.py
a
|
b
|
|
223 | 223 | try: |
224 | 224 | return getattr(instance, self.cache_name) |
225 | 225 | except AttributeError: |
| 226 | if instance._state.adding: |
| 227 | raise ValueError("Cannot retrieve related objects until the instance has been saved") |
| 228 | |
226 | 229 | params = {'%s__pk' % self.related.field.name: instance._get_pk_val()} |
227 | 230 | db = router.db_for_read(self.related.model, instance=instance) |
228 | 231 | rel_obj = self.related.model._base_manager.using(db).get(**params) |
… |
… |
|
454 | 457 | |
455 | 458 | manager = RelatedManager() |
456 | 459 | attname = rel_field.rel.get_related_field().name |
| 460 | |
| 461 | if instance._state.adding: |
| 462 | raise ValueError("Cannot retrieve related objects until the instance has been saved") |
457 | 463 | manager.core_filters = {'%s__%s' % (rel_field.name, attname): |
458 | 464 | getattr(instance, attname)} |
459 | 465 | manager.model = self.related.model |
… |
… |
|
478 | 484 | self.through = through |
479 | 485 | self._pk_val = self.instance.pk |
480 | 486 | self.reverse = reverse |
481 | | if self._pk_val is None: |
482 | | raise ValueError("%r instance needs to have a primary key value before a many-to-many relationship can be used." % instance.__class__.__name__) |
| 487 | if self.instance._state.adding: |
| 488 | raise ValueError("%r instance needs to be saved before a many-to-many relationship can be used." % instance.__class__.__name__) |
483 | 489 | |
484 | 490 | def get_query_set(self): |
485 | 491 | db = self._db or router.db_for_read(self.instance.__class__, instance=self.instance) |
diff -r 24c30016c52f tests/modeltests/lookup/tests.py
a
|
b
|
|
350 | 350 | '<Article: Article 2>') |
351 | 351 | self.assertEqual(repr(self.a2.get_previous_by_pub_date()), |
352 | 352 | '<Article: Article 1>') |
| 353 | self.assertRaises(ValueError, lambda: Article().get_previous_by_pub_date()) |
353 | 354 | |
354 | 355 | def test_escaping(self): |
355 | 356 | # Underscores, percent signs and backslashes have special meaning in the |
diff -r 24c30016c52f tests/modeltests/many_to_one/tests.py
a
|
b
|
|
370 | 370 | self.r.cached_query = Article.objects.filter(reporter=self.r) |
371 | 371 | from copy import deepcopy |
372 | 372 | self.assertEqual(repr(deepcopy(self.r)), "<Reporter: John Smith>") |
| 373 | |
| 374 | def test_related_before_save(self): |
| 375 | # You cannot get the related objects before the main object is saved. |
| 376 | # See ticket #14615 |
| 377 | self.assertRaises(ValueError, getattr, Reporter(), 'article_set') |
diff -r 24c30016c52f tests/modeltests/one_to_one/tests.py
a
|
b
|
|
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 -r 24c30016c52f tests/regressiontests/null_queries/tests.py
a
|
b
|
|
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.assertEquals(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 |