Essentially, the opposite of defer(). Only the fields passed into this method and that are not already specified as deferred are loaded immediately when the queryset is evaluated.
(self, *fields)
| 1948 | return clone |
| 1949 | |
| 1950 | def only(self, *fields): |
| 1951 | """ |
| 1952 | Essentially, the opposite of defer(). Only the fields passed into this |
| 1953 | method and that are not already specified as deferred are loaded |
| 1954 | immediately when the queryset is evaluated. |
| 1955 | """ |
| 1956 | self._not_support_combined_queries("only") |
| 1957 | if self._fields is not None: |
| 1958 | raise TypeError("Cannot call only() after .values() or .values_list()") |
| 1959 | if fields == (None,): |
| 1960 | # Can only pass None to defer(), not only(), as the rest option. |
| 1961 | # That won't stop people trying to do this, so let's be explicit. |
| 1962 | raise TypeError("Cannot pass None as an argument to only().") |
| 1963 | for field in fields: |
| 1964 | field = field.split(LOOKUP_SEP, 1)[0] |
| 1965 | if field in self.query._filtered_relations: |
| 1966 | raise ValueError("only() is not supported with FilteredRelation.") |
| 1967 | clone = self._chain() |
| 1968 | clone.query.add_immediate_loading(fields) |
| 1969 | return clone |
| 1970 | |
| 1971 | def using(self, alias): |
| 1972 | """Select which database this QuerySet should execute against.""" |