1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #24687 -- Added select_related() validation for nested non-relational fields.

The removed test was added in the original select_related() validation
patch (45d4e43d2d), but there doesn't
seem to be any reason for it.

Thanks Claude Paroz for help and review.
This commit is contained in:
Tim Graham
2015-10-09 21:03:04 -04:00
parent 5171f56fae
commit 67732a9b18
7 changed files with 31 additions and 13 deletions

View File

@@ -10,6 +10,7 @@ from django.contrib.gis.db.models.sql import (
from django.contrib.gis.geometry.backend import Geometry
from django.contrib.gis.measure import Area, Distance
from django.db import connections
from django.db.models.constants import LOOKUP_SEP
from django.db.models.expressions import RawSQL
from django.db.models.fields import Field
from django.db.models.query import QuerySet
@@ -675,9 +676,10 @@ class GeoQuerySet(QuerySet):
if geo_field not in opts.fields:
# Is this operation going to be on a related geographic field?
# If so, it'll have to be added to the select related information
# (e.g., if 'location__point' was given as the field name).
# (e.g., if 'location__point' was given as the field name, then
# chop the non-relational field and add select_related('location')).
# Note: the operation really is defined as "must add select related!"
self.query.add_select_related([field_name])
self.query.add_select_related([field_name.rsplit(LOOKUP_SEP, 1)[0]])
# Call pre_sql_setup() so that compiler.select gets populated.
compiler.pre_sql_setup()
for col, _, _ in compiler.select:

View File

@@ -673,7 +673,7 @@ class SQLCompiler(object):
if not f.is_relation:
# If a non-related field is used like a relation,
# or if a single non-relational field is given.
if next or (cur_depth == 1 and f.name in requested):
if next or f.name in requested:
raise FieldError(
"Non-relational field given in select_related: '%s'. "
"Choices are: %s" % (