Code

Ticket #17582: #17582.2.patch

File #17582.2.patch, 2.3 KB (added by JordanPowell, 13 months ago)
  • django/db/models/fields/related.py

    commit 5581baf0c79322fc2449b685859cd46666cae390
    Author: Jordan Rhys Powell <jordan.rhys.powell@gmail.com>
    Date:   Sun Mar 24 23:10:39 2013 +0000
    
        Fixed #17582 - Improve foreign field DoesNotExist message
        
        Added a message when accessing a foreign field fails due to a null or
        invalid value. The message includes the model containing the field and
        the field name.
    
    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index b575583..7ca701a 100644
    a b class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec 
    307307                    setattr(rel_obj, self.field.related.get_cache_name(), instance) 
    308308            setattr(instance, self.cache_name, rel_obj) 
    309309        if rel_obj is None and not self.field.null: 
    310             raise self.field.rel.to.DoesNotExist 
     310            raise self.field.rel.to.DoesNotExist( 
     311                "%s has no %s." % (self.field.model.__name__, self.field.name)) 
    311312        else: 
    312313            return rel_obj 
    313314 
  • docs/releases/1.6.txt

    diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt
    index 236be7b..8923cd2 100644
    a b Minor features 
    181181  and the undocumented limit of the higher of 1000 or ``max_num`` forms 
    182182  was changed so it is always 1000 more than ``max_num``. 
    183183 
     184* The ``DoesNotExist`` exception message now indicates which foreign field is null or invalid 
     185 
    184186Backwards incompatible changes in 1.6 
    185187===================================== 
    186188 
  • tests/foreign_object/tests.py

    diff --git a/tests/foreign_object/tests.py b/tests/foreign_object/tests.py
    index 2ca13cb..240f2a6 100644
    a b class MultiColumnFKTests(TestCase): 
    316316            list(Article.objects.filter(active_translation__abstract=None)), 
    317317            [a1, a2]) 
    318318 
     319    def test_foreign_key_raises_informative_does_not_exist(self): 
     320        referrer = ArticleTranslation() 
     321        with self.assertRaisesMessage(Article.DoesNotExist, 'ArticleTranslation has no article'): 
     322            referrer.article 
     323 
    319324class FormsTests(TestCase): 
    320325    # ForeignObjects should not have any form fields, currently the user needs 
    321326    # to manually deal with the foreignobject relation.