Django

Code

Changeset 8098

Show
Ignore:
Timestamp:
07/26/08 20:18:23 (6 months ago)
Author:
mtredinnick
Message:

Fixed #7530, #7716 -- When using select_related() and encountering a NULL
related object, populate the attribute correctly. Patch from Bastien Kleineidam.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/query.py

    r7938 r8098  
    786786    restricted = requested is not None 
    787787    index_end = index_start + len(klass._meta.fields) 
    788     obj = klass(*row[index_start:index_end]) 
     788    fields = row[index_start:index_end] 
     789    if not [x for x in fields if x is not None]: 
     790        # If we only have a list of Nones, there was not related object. 
     791        return None, index_end 
     792    obj = klass(*fields) 
    789793    for f in klass._meta.fields: 
    790794        if not select_related_descend(f, restricted, requested): 
  • django/trunk/tests/regressiontests/null_fk/models.py

    r7597 r8098  
    11""" 
    2 Regression tests for proper working of ForeignKey(null=True). Tests these bugs: 
    3  
    4     * #7369: FK non-null after null relationship on select_related() generates an invalid query 
    5  
     2Regression tests for proper working of ForeignKey(null=True). 
    63""" 
    74 
     
    3936# Starting from comment, make sure that a .select_related(...) with a specified 
    4037# set of fields will properly LEFT JOIN multiple levels of NULLs (and the things 
    41 # that come after the NULLs, or else data that should exist won't). 
     38# that come after the NULLs, or else data that should exist won't). Regression 
     39# test for #7369. 
    4240>>> c = Comment.objects.select_related().get(id=1) 
    4341>>> c.post 
     
    4846 
    4947>>> comments = Comment.objects.select_related('post__forum__system_info').all() 
    50 >>> [(c.id, c.post.id) for c in comments] 
    51 [(1, 1), (2, None)] 
    52 >>> [(c.comment_text, c.post.title) for c in comments] 
    53 [(u'My first comment', u'First Post'), (u'My second comment', None)] 
     48>>> [(c.id, c.comment_text, c.post) for c in comments] 
     49[(1, u'My first comment', <Post: First Post>), (2, u'My second comment', None)] 
     50 
     51# Regression test for #7530, #7716. 
     52>>> Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None 
     53True 
    5454 
    5555"""}