1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

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
This commit is contained in:
Russell Keith-Magee 2006-03-12 04:10:57 +00:00
parent a5bd0543f1
commit 7a0b04fdc3
3 changed files with 12 additions and 14 deletions

View File

@ -117,6 +117,9 @@ class ReverseSingleRelatedObjectDescriptor(object):
except AttributeError:
val = getattr(instance, self.field.attname)
if val is None:
# If Null is an allowed value, return it.
if self.field.null:
return None
raise self.field.rel.to.DoesNotExist
other_field = self.field.rel.get_related_field()
if other_field.rel:

View File

@ -30,10 +30,8 @@ API_TESTS = """
[Child category]
>>> r.child_set.get(name__startswith='Child')
Child category
>>> r.parent
Traceback (most recent call last):
...
DoesNotExist
>>> print r.parent
None
>>> c.child_set.all()
[]

View File

@ -61,23 +61,20 @@ Second
>>> a3.save()
>>> a3.id
3
>>> a3.reporter
Traceback (most recent call last):
...
DoesNotExist
>>> print a3.reporter
None
# Need to reget a3 to refresh the cache
>>> a3 = Article.objects.get(pk=3)
>>> print a3.reporter.id
Traceback (most recent call last):
...
DoesNotExist
AttributeError: 'NoneType' object has no attribute 'id'
# Accessing an article's 'reporter' attribute throws ReporterDoesNotExist
# Accessing an article's 'reporter' attribute returns None
# if the reporter is set to None.
>>> a3.reporter
Traceback (most recent call last):
...
DoesNotExist
>>> print a3.reporter
None
# To retrieve the articles with no reporters set, use "reporter__isnull=True".
>>> Article.objects.filter(reporter__isnull=True)