Loading docs/ref/models/instances.txt +12 −0 Original line number Diff line number Diff line Loading @@ -128,6 +128,18 @@ in the ``from_db()`` method. Refreshing objects from database ================================ If you delete a field from a model instance, accessing it again reloads the value from the database:: >>> obj = MyModel.objects.first() >>> del obj.field >>> obj.field # Loads the field from the database .. versionchanged:: 1.10 In older versions, accessing a deleted field raised ``AttributeError`` instead of reloading it. .. method:: Model.refresh_from_db(using=None, fields=None) If you need to reload a model's values from the database, you can use the Loading docs/releases/1.10.txt +3 −0 Original line number Diff line number Diff line Loading @@ -884,6 +884,9 @@ Miscellaneous * The ``_base_manager`` and ``_default_manager`` attributes are removed from model instances. They remain accessible on the model class. * Accessing a deleted field on a model instance, e.g. after ``del obj.field``, reloads the field's value instead of raising ``AttributeError``. .. _deprecated-features-1.10: Features deprecated in 1.10 Loading tests/basic/tests.py +13 −0 Original line number Diff line number Diff line Loading @@ -421,6 +421,19 @@ class ModelTest(TestCase): # hash) hash(Article()) def test_delete_and_access_field(self): # Accessing a field after it's deleted from a model reloads its value. pub_date = datetime.now() article = Article.objects.create(headline='foo', pub_date=pub_date) new_pub_date = article.pub_date + timedelta(days=10) article.headline = 'bar' article.pub_date = new_pub_date del article.headline with self.assertNumQueries(1): self.assertEqual(article.headline, 'foo') # Fields that weren't deleted aren't reloaded. self.assertEqual(article.pub_date, new_pub_date) class ModelLookupTest(TestCase): def setUp(self): Loading Loading
docs/ref/models/instances.txt +12 −0 Original line number Diff line number Diff line Loading @@ -128,6 +128,18 @@ in the ``from_db()`` method. Refreshing objects from database ================================ If you delete a field from a model instance, accessing it again reloads the value from the database:: >>> obj = MyModel.objects.first() >>> del obj.field >>> obj.field # Loads the field from the database .. versionchanged:: 1.10 In older versions, accessing a deleted field raised ``AttributeError`` instead of reloading it. .. method:: Model.refresh_from_db(using=None, fields=None) If you need to reload a model's values from the database, you can use the Loading
docs/releases/1.10.txt +3 −0 Original line number Diff line number Diff line Loading @@ -884,6 +884,9 @@ Miscellaneous * The ``_base_manager`` and ``_default_manager`` attributes are removed from model instances. They remain accessible on the model class. * Accessing a deleted field on a model instance, e.g. after ``del obj.field``, reloads the field's value instead of raising ``AttributeError``. .. _deprecated-features-1.10: Features deprecated in 1.10 Loading
tests/basic/tests.py +13 −0 Original line number Diff line number Diff line Loading @@ -421,6 +421,19 @@ class ModelTest(TestCase): # hash) hash(Article()) def test_delete_and_access_field(self): # Accessing a field after it's deleted from a model reloads its value. pub_date = datetime.now() article = Article.objects.create(headline='foo', pub_date=pub_date) new_pub_date = article.pub_date + timedelta(days=10) article.headline = 'bar' article.pub_date = new_pub_date del article.headline with self.assertNumQueries(1): self.assertEqual(article.headline, 'foo') # Fields that weren't deleted aren't reloaded. self.assertEqual(article.pub_date, new_pub_date) class ModelLookupTest(TestCase): def setUp(self): Loading