Opened 5 years ago

Closed 4 years ago

#14639 closed (fixed)

admin.site.register() does not check if the model is abstract (gives weird error)

Reported by: vdboor Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords: error-messages
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

When an abstract model is registered with admin.site.register() it produces the following error later on:

type object '...' has no attribute '_default_manager'

This error does not make much sense unless you're already have more intimate knowledge of the framework.
To ease the learning curve of new developers, I think it would make sense to check whether the model is abstract or not.

Attachments (1)

14639-admin-site-prevent-abstract-registration.patch (2.2 KB) - added by cdestigter 5 years ago.
Prevent registration of abstract models with AdminSite.register(). Includes regression test

Download all attachments as: .zip

Change History (6)

comment:1 Changed 5 years ago by vdboor

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

Addition: it produces even more weird errors in the admin_tools dashboard, when viewing a specific app:

Caught AttributeError while rendering: 'NoneType' object has no attribute '_meta'

which happens somewhere in the contenttypes framework:

File "/usr/lib/python2.6/site-packages/admin_tools/dashboard/templatetags/admin_tools_dashboard_tags.py" in admin_tools_render_dashboard_css
  90.         dashboard = get_dashboard(context, location)
File "/usr/lib/python2.6/site-packages/admin_tools/dashboard/utils.py" in get_dashboard
  21.         return get_app_index_dashboard(context)
File "/usr/lib/python2.6/site-packages/admin_tools/dashboard/utils.py" in get_app_index_dashboard
  71.     return getattr(mod, inst)(app_title, model_list)
File "/usr/lib/python2.6/site-packages/admin_tools/dashboard/dashboards.py" in __init__
  304.             include_list=self.get_app_content_types(),
File "/usr/lib/python2.6/site-packages/admin_tools/dashboard/dashboards.py" in get_app_content_types
  184.                 in self.get_app_model_classes()]
File "/usr/lib/python2.6/site-packages/django/contrib/contenttypes/models.py" in get_for_model
  40.             self._add_to_cache(self.db, ct)
File "/usr/lib/python2.6/site-packages/django/contrib/contenttypes/models.py" in _add_to_cache
  70.         key = (model._meta.app_label, model._meta.object_name.lower())

comment:2 Changed 5 years ago by rasca

  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.2 to SVN

I can confirm that it outputs this error.

Changed 5 years ago by cdestigter

Prevent registration of abstract models with AdminSite.register(). Includes regression test

comment:3 Changed 5 years ago by cdestigter

  • Has patch set

comment:4 Changed 5 years ago by jezdez

  • Needs tests set

comment:5 Changed 4 years ago by julien

  • Resolution set to fixed
  • Status changed from new to closed

This was fixed in [15636].

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