mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09: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:
parent
a5bd0543f1
commit
7a0b04fdc3
@ -117,6 +117,9 @@ class ReverseSingleRelatedObjectDescriptor(object):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
val = getattr(instance, self.field.attname)
|
val = getattr(instance, self.field.attname)
|
||||||
if val is None:
|
if val is None:
|
||||||
|
# If Null is an allowed value, return it.
|
||||||
|
if self.field.null:
|
||||||
|
return None
|
||||||
raise self.field.rel.to.DoesNotExist
|
raise self.field.rel.to.DoesNotExist
|
||||||
other_field = self.field.rel.get_related_field()
|
other_field = self.field.rel.get_related_field()
|
||||||
if other_field.rel:
|
if other_field.rel:
|
||||||
|
@ -30,10 +30,8 @@ API_TESTS = """
|
|||||||
[Child category]
|
[Child category]
|
||||||
>>> r.child_set.get(name__startswith='Child')
|
>>> r.child_set.get(name__startswith='Child')
|
||||||
Child category
|
Child category
|
||||||
>>> r.parent
|
>>> print r.parent
|
||||||
Traceback (most recent call last):
|
None
|
||||||
...
|
|
||||||
DoesNotExist
|
|
||||||
|
|
||||||
>>> c.child_set.all()
|
>>> c.child_set.all()
|
||||||
[]
|
[]
|
||||||
|
@ -61,23 +61,20 @@ Second
|
|||||||
>>> a3.save()
|
>>> a3.save()
|
||||||
>>> a3.id
|
>>> a3.id
|
||||||
3
|
3
|
||||||
>>> a3.reporter
|
>>> print a3.reporter
|
||||||
Traceback (most recent call last):
|
None
|
||||||
...
|
|
||||||
DoesNotExist
|
|
||||||
|
|
||||||
|
# Need to reget a3 to refresh the cache
|
||||||
>>> a3 = Article.objects.get(pk=3)
|
>>> a3 = Article.objects.get(pk=3)
|
||||||
>>> print a3.reporter.id
|
>>> print a3.reporter.id
|
||||||
Traceback (most recent call last):
|
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.
|
# if the reporter is set to None.
|
||||||
>>> a3.reporter
|
>>> print a3.reporter
|
||||||
Traceback (most recent call last):
|
None
|
||||||
...
|
|
||||||
DoesNotExist
|
|
||||||
|
|
||||||
# To retrieve the articles with no reporters set, use "reporter__isnull=True".
|
# To retrieve the articles with no reporters set, use "reporter__isnull=True".
|
||||||
>>> Article.objects.filter(reporter__isnull=True)
|
>>> Article.objects.filter(reporter__isnull=True)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user