Opened 14 months ago

Closed 14 months ago

Last modified 14 months ago

#22994 closed Bug (fixed)

"'GenericForeignKey' object has no attribute 'opts'" exception on 1.7

Reported by: glicerinu@… Owned by: Tim Graham <timograham@…>
Component: contrib.contenttypes Version: 1.7-rc-1
Severity: Release blocker Keywords: GenericForeignKey content_object opts list_display
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django raises a "'GenericForeignKey' object has no attribute 'opts'" when using 'content_object' on a ModelAdmin.list_display

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  137.                 response = response.render()
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render
  103.             self.content = self.rendered_content
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in rendered_content
  80.         content = template.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  148.             return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/test/utils.py" in instrumented_test_render
  88.     return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/test/utils.py" in instrumented_test_render
  88.     return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/test/utils.py" in instrumented_test_render
  88.     return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  1207.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  307.     headers = list(result_headers(cl))
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templatetags/admin_list.py" in result_headers
  101.             return_attr=True
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/utils.py" in label_for_field
  305.             label = field.opts.verbose_name

Exception Type: AttributeError at /admin/resources/resourcedata/
Exception Value: 'GenericForeignKey' object has no attribute 'opts'

Attachments (1)

22994-test.diff (2.8 KB) - added by timo 14 months ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 14 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Did this work in 1.6?

comment:2 Changed 14 months ago by glicerinu@…

Yes, this error appeared after upgrading to 1.7

comment:3 Changed 14 months ago by timo

  • Severity changed from Normal to Release blocker
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 14 months ago by timo

  • Owner changed from nobody to timo
  • Status changed from new to assigned

Changed 14 months ago by timo

comment:6 Changed 14 months ago by timo

  • Owner timo deleted
  • Status changed from assigned to new

Added test which passes on 1.6 and fails on master & 1.7.

comment:7 Changed 14 months ago by timo

  • Has patch set
  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 14 months ago by Tim Graham <timograham@…>

  • Owner set to Tim Graham <timograham@…>
  • Resolution set to fixed
  • Status changed from new to closed

In 9cd5201abd24ba2632753029ee1957947cdc32f5:

Fixed #22994 -- regression with generic FK + admin list_view

The reason for the regression was that the GenericForeignKey field isn't
something meta.get_field_by_name() should return. The reason is that a
couple of places in Django expects get_field_by_name() to work this way.
It could make sense to return GFKs from get_field_by_name(), but that
should likely be done as part of meta refactoring or virtual fields
refactoring patches.

Thanks to glicerinu@… for the report and to Tim for working on
the issue.

comment:9 Changed 14 months ago by Tim Graham <timograham@…>

In 572885729e028eae2f2b823ef87543b7c66bdb10:

[1.7.x] Fixed #22994 -- regression with generic FK + admin list_view

The reason for the regression was that the GenericForeignKey field isn't
something meta.get_field_by_name() should return. The reason is that a
couple of places in Django expects get_field_by_name() to work this way.
It could make sense to return GFKs from get_field_by_name(), but that
should likely be done as part of meta refactoring or virtual fields
refactoring patches.

Thanks to glicerinu@… for the report and to Tim for working on
the issue.

Note: See TracTickets for help on using tickets.
Back to Top