Ticket #10811: 10811-2.patch

File 10811-2.patch, 5.6 KB (added by aaugustin, 18 months ago)
  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 4caed4f..937e9d3 100644
    a b class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec 
    331331                if not router.allow_relation(value, instance):
    332332                    raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value)
    333333
     334            pk = tuple(getattr(value, field.attname, None) for _, field in self.field.related_fields)
     335            if None in pk and self.field.null is False:
     336                raise ValueError('Cannot assign "%r": "%s" instance isn\'t saved in the database.' %
     337                                    (value, self.field.rel.to._meta.object_name))
     338
    334339        # If we're setting the value of a OneToOneField to None, we need to clear
    335340        # out the cache on any old related object. Otherwise, deleting the
    336341        # previously-related object will also cause this object to be deleted,
    class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec 
    352357
    353358        # Set the value of the related field
    354359        for lh_field, rh_field in self.field.related_fields:
    355             try:
    356                 setattr(instance, lh_field.attname, getattr(value, rh_field.attname))
    357             except AttributeError:
    358                 setattr(instance, lh_field.attname, None)
     360            setattr(instance, lh_field.attname, getattr(value, rh_field.attname, None))
    359361
    360362        # Since we already know what the related object is, seed the related
    361363        # object caches now, too. This avoids another db hit if you get the
  • tests/admin_util/tests.py

    diff --git a/tests/admin_util/tests.py b/tests/admin_util/tests.py
    index 8c63e90..859967b 100644
    a b class UtilTests(SimpleTestCase): 
    100100        simple_function = lambda obj: SIMPLE_FUNCTION
    101101
    102102        article = Article(
    103             site=Site(domain=SITE_NAME),
     103            site=Site.objects.create(domain=SITE_NAME),
    104104            title=TITLE_TEXT,
    105105            created=CREATED_DATE,
    106106        )
  • tests/many_to_one_regress/tests.py

    diff --git a/tests/many_to_one_regress/tests.py b/tests/many_to_one_regress/tests.py
    index 19cfaad..7ad8590 100644
    a b class ManyToOneRegressionTests(TestCase): 
    6565        c = Child(parent=p)
    6666        self.assertTrue(c.parent is p)
    6767
    68         # Creation using keyword argument and unsaved related instance (#8070).
    69         p = Parent()
    70         c = Child(parent=p)
    71         self.assertTrue(c.parent is p)
    72 
    7368        # Creation using attname keyword argument and an id will cause the
    7469        # related object to be fetched.
    7570        p = Parent.objects.get(name="Parent")
  • tests/model_formsets/tests.py

    diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
    index 0534481..dc4258e 100644
    a b class ModelFormsetTest(TestCase): 
    548548            'book_set-2-title': '',
    549549        }
    550550
    551         formset = AuthorBooksFormSet(data, instance=Author(), save_as_new=True)
     551        new_author = Author.objects.create(name='Charles Baudelaire')
     552
     553        formset = AuthorBooksFormSet(data, instance=new_author, save_as_new=True)
    552554        self.assertTrue(formset.is_valid())
    553555
    554         new_author = Author.objects.create(name='Charles Baudelaire')
    555556        formset = AuthorBooksFormSet(data, instance=new_author, save_as_new=True)
    556557        saved = formset.save()
    557558        self.assertEqual(len(saved), 2)
  • tests/model_regress/tests.py

    diff --git a/tests/model_regress/tests.py b/tests/model_regress/tests.py
    index f84a40b..c1f1b97 100644
    a b import sys 
    66import unittest
    77
    88from django.core.exceptions import ValidationError
    9 from django.test import TestCase, skipUnlessDBFeature
    10 from django.utils import six
    11 from django.utils import tzinfo
    129from django.db import connection, router
    1310from django.db.models.sql import InsertQuery
     11from django.test import TestCase, skipUnlessDBFeature
     12from django.utils import six, tzinfo
    1413
    1514from .models import (Worker, Article, Party, Event, Department,
    1615    BrokenUnicodeMethod, NonAutoPK, Model1, Model2, Model3)
    class ModelTests(TestCase): 
    214213        m3 = Model3.objects.get(model2=1000)
    215214        m3.model2
    216215
     216    def test_assign_unsaved_instance_to_fk(self):
     217        """
     218        Regression for #10811: Can't assign an unsaved instance to a FK
     219        """
     220        w = Worker(name="Joe")
     221        d = Department(name="Accounting")       # not saved in the database
     222        with six.assertRaisesRegex(self, ValueError,
     223                'Cannot assign "<Department: Accounting>": '
     224                '"Department" instance isn\'t saved in the database.'):
     225            w.department = d
     226
    217227
    218228class ModelValidationTest(TestCase):
    219229    def test_pk_validation(self):
  • tests/one_to_one_regress/tests.py

    diff --git a/tests/one_to_one_regress/tests.py b/tests/one_to_one_regress/tests.py
    index 55836d4..07a205c 100644
    a b class OneToOneRegressionTests(TestCase): 
    9494        r = Restaurant(place=p)
    9595        self.assertTrue(r.place is p)
    9696
    97         # Creation using keyword argument and unsaved related instance (#8070).
    98         p = Place()
    99         r = Restaurant(place=p)
    100         self.assertTrue(r.place is p)
    101 
    10297        # Creation using attname keyword argument and an id will cause the related
    10398        # object to be fetched.
    10499        p = Place.objects.get(name="Demon Dogs")
Back to Top