~ubuntuone-pqm-team/django/stable

« back to all changes in this revision

Viewing changes to django/db/models/query.py

  • Committer: Natalia
  • Date: 2016-05-03 13:51:18 UTC
  • Revision ID: natalia.bidart@ubuntu.com-20160503135118-5yv5ywnfpqv6onnc
- Imported Django 1.9.6 from released tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
from django.db.models import sql
17
17
from django.db.models.constants import LOOKUP_SEP
18
18
from django.db.models.deletion import Collector
19
 
from django.db.models.expressions import F, Date, DateTime
 
19
from django.db.models.expressions import Date, DateTime, F
20
20
from django.db.models.fields import AutoField
21
21
from django.db.models.query_utils import (
22
 
    Q, InvalidQuery, check_rel_lookup_compatibility, deferred_class_factory,
 
22
    InvalidQuery, Q, check_rel_lookup_compatibility, deferred_class_factory,
23
23
)
24
24
from django.db.models.sql.constants import CURSOR
25
25
from django.utils import six, timezone
1107
1107
        for field, objects in other._known_related_objects.items():
1108
1108
            self._known_related_objects.setdefault(field, {}).update(objects)
1109
1109
 
1110
 
    def _prepare(self):
 
1110
    def _prepare(self, field):
1111
1111
        if self._fields is not None:
1112
1112
            # values() queryset can only be used as nested queries
1113
1113
            # if they are set up to select only a single field.
1114
1114
            if len(self._fields or self.model._meta.concrete_fields) > 1:
1115
1115
                raise TypeError('Cannot use multi-field values as a filter value.')
 
1116
        elif self.model != field.model:
 
1117
            # If the query is used as a subquery for a ForeignKey with non-pk
 
1118
            # target field, make sure to select the target field in the subquery.
 
1119
            foreign_fields = getattr(field, 'foreign_related_fields', ())
 
1120
            if len(foreign_fields) == 1 and not foreign_fields[0].primary_key:
 
1121
                return self.values(foreign_fields[0].name)
1116
1122
        return self
1117
1123
 
1118
1124
    def _as_sql(self, connection):