For historical reasons, the admin app relies on GenericForeignKeys as being "not found" by get_field(). This could likely be cleaned up. Reverse relations should also be excluded as these aren't attributes of the model (rather something like `foo_set`).
(opts, name)
| 327 | |
| 328 | |
| 329 | def _get_non_gfk_field(opts, name): |
| 330 | """ |
| 331 | For historical reasons, the admin app relies on GenericForeignKeys as being |
| 332 | "not found" by get_field(). This could likely be cleaned up. |
| 333 | |
| 334 | Reverse relations should also be excluded as these aren't attributes of the |
| 335 | model (rather something like `foo_set`). |
| 336 | """ |
| 337 | field = opts.get_field(name) |
| 338 | if ( |
| 339 | field.is_relation |
| 340 | and |
| 341 | # Generic foreign keys OR reverse relations |
| 342 | ((field.many_to_one and not field.related_model) or field.one_to_many) |
| 343 | ): |
| 344 | raise FieldDoesNotExist() |
| 345 | |
| 346 | # Avoid coercing <FK>_id fields to FK |
| 347 | if ( |
| 348 | field.is_relation |
| 349 | and not field.many_to_many |
| 350 | and hasattr(field, "attname") |
| 351 | and field.attname == name |
| 352 | ): |
| 353 | raise FieldIsAForeignKeyColumnName() |
| 354 | |
| 355 | return field |
| 356 | |
| 357 | |
| 358 | def label_for_field(name, model, model_admin=None, return_attr=False, form=None): |
no test coverage detected