Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

magic-removal: Modified descriptor to return None rather than raising…

… DoesNotExist if null=True for a related field.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2511 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7a0b04fdc3abb8ff4db7ffc81d4a0201f682a82a 1 parent a5bd054
Russell Keith-Magee authored March 12, 2006
3  django/db/models/fields/related.py
@@ -117,6 +117,9 @@ def __get__(self, instance, instance_type=None):
6  tests/modeltests/m2o_recursive/models.py
@@ -30,10 +30,8 @@ def __repr__(self):
30 30
 [Child category]
31 31
 >>> r.child_set.get(name__startswith='Child')
32 32
 Child category
33  
->>> r.parent
34  
-Traceback (most recent call last):
35  
-    ...
36  
-DoesNotExist
  33
+>>> print r.parent
  34
+None
37 35
 
38 36
 >>> c.child_set.all()
39 37
 []
17  tests/modeltests/many_to_one_null/models.py
@@ -61,23 +61,20 @@ class Meta:
61 61
 >>> a3.save()
62 62
 >>> a3.id
63 63
 3
64  
->>> a3.reporter
65  
-Traceback (most recent call last):
66  
-    ...
67  
-DoesNotExist
  64
+>>> print a3.reporter
  65
+None
68 66
 
  67
+# Need to reget a3 to refresh the cache
69 68
 >>> a3 = Article.objects.get(pk=3)
70 69
 >>> print a3.reporter.id
71 70
 Traceback (most recent call last):
72 71
     ...
73  
-DoesNotExist
  72
+AttributeError: 'NoneType' object has no attribute 'id'
74 73
 
75  
-# Accessing an article's 'reporter' attribute throws ReporterDoesNotExist
  74
+# Accessing an article's 'reporter' attribute returns None
76 75
 # if the reporter is set to None.
77  
->>> a3.reporter
78  
-Traceback (most recent call last):
79  
-    ...
80  
-DoesNotExist
  76
+>>> print a3.reporter
  77
+None
81 78
 
82 79
 # To retrieve the articles with no reporters set, use "reporter__isnull=True".
83 80
 >>> Article.objects.filter(reporter__isnull=True)

0 notes on commit 7a0b04f

Please sign in to comment.
Something went wrong with that request. Please try again.