Add the given (model) fields to the select set. Add the field names in the order specified.
(self, field_names, allow_m2m=True)
| 2265 | self.distinct = True |
| 2266 | |
| 2267 | def add_fields(self, field_names, allow_m2m=True): |
| 2268 | """ |
| 2269 | Add the given (model) fields to the select set. Add the field names in |
| 2270 | the order specified. |
| 2271 | """ |
| 2272 | alias = self.get_initial_alias() |
| 2273 | opts = self.get_meta() |
| 2274 | |
| 2275 | try: |
| 2276 | cols = [] |
| 2277 | for name in field_names: |
| 2278 | # Join promotion note - we must not remove any rows here, so |
| 2279 | # if there is no existing joins, use outer join. |
| 2280 | join_info = self.setup_joins( |
| 2281 | name.split(LOOKUP_SEP), opts, alias, allow_many=allow_m2m |
| 2282 | ) |
| 2283 | targets, final_alias, joins = self.trim_joins( |
| 2284 | join_info.targets, |
| 2285 | join_info.joins, |
| 2286 | join_info.path, |
| 2287 | ) |
| 2288 | if len(targets) > 1: |
| 2289 | transformed_targets = [ |
| 2290 | join_info.transform_function(target, final_alias) |
| 2291 | for target in targets |
| 2292 | ] |
| 2293 | cols.append( |
| 2294 | ColPairs( |
| 2295 | final_alias if self.alias_cols else None, |
| 2296 | [col.target for col in transformed_targets], |
| 2297 | [col.output_field for col in transformed_targets], |
| 2298 | join_info.final_field, |
| 2299 | ) |
| 2300 | ) |
| 2301 | else: |
| 2302 | cols.append(join_info.transform_function(targets[0], final_alias)) |
| 2303 | if cols: |
| 2304 | self.set_select(cols) |
| 2305 | except MultiJoin: |
| 2306 | raise FieldError("Invalid field name: '%s'" % name) |
| 2307 | except FieldError: |
| 2308 | if LOOKUP_SEP in name: |
| 2309 | # For lookups spanning over relationships, show the error |
| 2310 | # from the model on which the lookup failed. |
| 2311 | raise |
| 2312 | else: |
| 2313 | names = sorted( |
| 2314 | [ |
| 2315 | *get_field_names_from_opts(opts), |
| 2316 | *self.extra, |
| 2317 | *self.annotation_select, |
| 2318 | *self._filtered_relations, |
| 2319 | ] |
| 2320 | ) |
| 2321 | raise FieldError( |
| 2322 | "Cannot resolve keyword %r into field. " |
| 2323 | "Choices are: %s" % (name, ", ".join(names)) |
| 2324 | ) |
no test coverage detected