Django

Code

Changeset 2511

Show
Ignore:
Timestamp:
03/11/06 22:10:57 (3 years ago)
Author:
russellm
Message:

magic-removal: Modified descriptor to return None rather than raising DoesNotExist? if null=True for a related field.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/db/models/fields/related.py

    r2510 r2511  
    118118            val = getattr(instance, self.field.attname) 
    119119            if val is None: 
     120                # If Null is an allowed value, return it. 
     121                if self.field.null: 
     122                    return None 
    120123                raise self.field.rel.to.DoesNotExist 
    121124            other_field = self.field.rel.get_related_field() 
  • django/branches/magic-removal/tests/modeltests/m2o_recursive/models.py

    r2251 r2511  
    3131>>> r.child_set.get(name__startswith='Child') 
    3232Child category 
    33 >>> r.parent 
    34 Traceback (most recent call last): 
    35     ... 
    36 DoesNotExist 
     33>>> print r.parent 
     34None 
    3735 
    3836>>> c.child_set.all() 
  • django/branches/magic-removal/tests/modeltests/many_to_one_null/models.py

    r2510 r2511  
    6262>>> a3.id 
    63633 
    64 >>> a3.reporter 
    65 Traceback (most recent call last): 
    66     ... 
    67 DoesNotExist 
     64>>> print a3.reporter 
     65None 
    6866 
     67# Need to reget a3 to refresh the cache 
    6968>>> a3 = Article.objects.get(pk=3) 
    7069>>> print a3.reporter.id 
    7170Traceback (most recent call last): 
    7271    ... 
    73 DoesNotExist 
     72AttributeError: 'NoneType' object has no attribute 'id' 
    7473 
    75 # Accessing an article's 'reporter' attribute throws ReporterDoesNotExist 
     74# Accessing an article's 'reporter' attribute returns None 
    7675# if the reporter is set to None. 
    77 >>> a3.reporter 
    78 Traceback (most recent call last): 
    79     ... 
    80 DoesNotExist 
     76>>> print a3.reporter 
     77None 
    8178 
    8279# To retrieve the articles with no reporters set, use "reporter__isnull=True".