Ticket #14615: 14615-r16341.diff

File 14615-r16341.diff, 5.1 KB (added by Victor van den Elzen <victor.van.den.elzen@…>, 4 years ago)

updated patch

  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index 9cf0834..8338a18 100644
    a b class Model(object): 
    590590        return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True)
    591591
    592592    def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
    593         if not self.pk:
     593        if self._state.adding:
    594594            raise ValueError("get_next/get_previous cannot be used on unsaved objects.")
    595595        op = is_next and 'gt' or 'lt'
    596596        order = not is_next and '-' or ''
  • django/db/models/fields/related.py

    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): 
    235235        try:
    236236            return getattr(instance, self.cache_name)
    237237        except AttributeError:
     238            if instance._state.adding:
     239                raise ValueError("Cannot retrieve related objects until the instance has been saved")
     240
    238241            params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
    239242            db = router.db_for_read(self.related.model, instance=instance)
    240243            rel_obj = self.related.model._base_manager.using(db).get(**params)
    class ForeignRelatedObjectsDescriptor(object): 
    464467
    465468        manager = RelatedManager()
    466469        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")
    467473        manager.core_filters = {'%s__%s' % (rel_field.name, attname):
    468474                getattr(instance, attname)}
    469475        manager.model = self.related.model
    def create_many_related_manager(superclass, rel=False): 
    488494            self.through = through
    489495            self._pk_val = self.instance.pk
    490496            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__)
    493499
    494500        def get_query_set(self):
    495501            db = self._db or router.db_for_read(self.instance.__class__, instance=self.instance)
  • tests/modeltests/lookup/tests.py

    diff --git a/tests/modeltests/lookup/tests.py b/tests/modeltests/lookup/tests.py
    index 3f40bf1..2f59202 100644
    a b class LookupTests(TestCase): 
    351351                         '<Article: Article 2>')
    352352        self.assertEqual(repr(self.a2.get_previous_by_pub_date()),
    353353                         '<Article: Article 1>')
     354        self.assertRaises(ValueError, lambda: Article().get_previous_by_pub_date())
    354355
    355356    def test_escaping(self):
    356357        # Underscores, percent signs and backslashes have special meaning in the
  • tests/modeltests/many_to_one/tests.py

    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): 
    372372        # recursive don't cause recursion depth problems under deepcopy.
    373373        self.r.cached_query = Article.objects.filter(reporter=self.r)
    374374        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')
  • tests/modeltests/one_to_one/tests.py

    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): 
    117117        mm = MultiModel(link1=self.p2, link2=o1, name="x1")
    118118        self.assertRaises(IntegrityError, mm.save)
    119119        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')
  • tests/regressiontests/null_queries/tests.py

    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): 
    4242        # Can't use None on anything other than __exact
    4343        self.assertRaises(ValueError, Choice.objects.filter, foo__gt=None)
    4444
    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 
    4945    def test_reverse_relations(self):
    5046        """
    5147        Querying across reverse relations and then another relation should
Back to Top