MCPcopy
hub / github.com/django/django / _get_non_gfk_field

Function _get_non_gfk_field

django/contrib/admin/utils.py:329–355  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

327
328
329def _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
358def label_for_field(name, model, model_admin=None, return_attr=False, form=None):

Callers 3

lookup_fieldFunction · 0.85
label_for_fieldFunction · 0.85
help_text_for_fieldFunction · 0.85

Calls 3

FieldDoesNotExistClass · 0.90
get_fieldMethod · 0.45

Tested by

no test coverage detected