Ticket #7530: django_related_null_fields_fix.patch

File django_related_null_fields_fix.patch, 1.4 KB (added by Bastian Kleineidam <calvin@…>, 7 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index cea443a..9144b2a 100644
    a b def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0, 
    752752
    753753    restricted = requested is not None
    754754    index_end = index_start + len(klass._meta.fields)
    755     obj = klass(*row[index_start:index_end])
     755    fields = row[index_start:index_end]
     756    if not [x for x in fields if x is not None]:
     757        # some select_related() queries return a list of None objects
     758        return None, index_end
     759    obj = klass(*fields)
    756760    for f in klass._meta.fields:
    757761        if (not f.rel or (not restricted and f.null) or
    758762                (restricted and f.name not in requested) or f.rel.parent_link):
  • tests/regressiontests/null_fk/models.py

    diff --git a/tests/regressiontests/null_fk/models.py b/tests/regressiontests/null_fk/models.py
    index 1bc266c..e7d748d 100644
    a b __test__ = {'API_TESTS':""" 
    4747None
    4848
    4949>>> 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)]
     50>>> [(c.id, c.comment_text, c.post) for c in comments]
     51[(1, u'My first comment', <Post: First Post>), (2, u'My second comment', None)]
    5452
    5553"""}
Back to Top