Opened 6 months ago

Closed 6 months ago

Last modified 6 months ago

#35056 closed Bug (fixed)

admin.E013 on ManyToManyField with related_name crashes.

Reported by: Thomas Feldmann Owned by: Mariusz Felisiak
Component: contrib.admin Version: 5.0
Severity: Release blocker Keywords:
Cc: Hrushikesh Vaidya Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm having problems updating my Django 4.2 project to Django 5.
Trying manage.py runserver raises an exception when performing system checks:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1045, in _bootstrap_inner
    self.run()
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 982, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/core/management/commands/runserver.py", line 133, in inner_run
    self.check(display_num_errors=True)
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/core/management/base.py", line 485, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/contrib/admin/checks.py", line 52, in check_admin_app
    errors.extend(site.check(app_configs))
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/contrib/admin/sites.py", line 89, in check
    errors.extend(modeladmin.check())
                  ^^^^^^^^^^^^^^^^^^
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/contrib/admin/options.py", line 148, in check
    return self.checks_class().check(self, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/contrib/admin/checks.py", line 806, in check
    *super().check(admin_obj),
     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/contrib/admin/checks.py", line 187, in check
    *self._check_filter_horizontal(admin_obj),
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/contrib/admin/checks.py", line 515, in _check_filter_horizontal
    return list(
           ^^^^^
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/contrib/admin/checks.py", line 517, in <genexpr>
    self._check_filter_item(
  File "/Users/tf/work/mama2/mama/.venv/lib/python3.11/site-packages/django/contrib/admin/checks.py", line 539, in _check_filter_item
    elif not field.remote_field.through._meta.auto_created:
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'ManyToManyField' object has no attribute 'through'

The project works fine in Django 4.2.7.
I found this comes from a filter_horizontal field which is a ManyToManyField's related_name.

Attachments (1)

Archiv.zip (17.1 KB ) - added by Thomas Feldmann 6 months ago.
Small reproduction example

Download all attachments as: .zip

Change History (9)

comment:1 by Mariusz Felisiak, 6 months ago

Thanks for the report. Can you provide a small project that reproduces the issue?

by Thomas Feldmann, 6 months ago

Attachment: Archiv.zip added

Small reproduction example

in reply to:  1 comment:2 by Thomas Feldmann, 6 months ago

Replying to Mariusz Felisiak:

Thanks for the report. Can you provide a small project that reproduces the issue?

Sure. Added a small example. While putting this together I realized this also doesn't work in Django 4 but does not raise an exception.
The error message could be a little clearer but I'm not sure whether this is a bug or a feature.

comment:3 by Mariusz Felisiak, 6 months ago

Cc: Hrushikesh Vaidya added
Severity: NormalRelease blocker
Summary: AttributeError: 'ManyToManyField' object has no attribute 'through'admin.E013 on ManyToManyField with related_name crashes.
Triage Stage: UnreviewedAccepted

Thanks for the report! ManyToManyFields with the through are generally not supported in the admin (#12203) but this should not crash. It seems that ManyToManyRel is recognized as many_to_many but it's remote_field doesn't have through

Regression in 107865780aa44914e21d27fdf4ca269bc61c7f01

comment:4 by Salvo Polizzi, 6 months ago

Can i help with this issue? If anyone can give me some advice or reference to other similar issues, I would like to contribute

in reply to:  4 comment:5 by Hrushikesh Vaidya, 6 months ago

Could this be fixed with a simple hasattr(field.remote_field, 'through') check before accessing through?

comment:6 by Mariusz Felisiak, 6 months ago

Has patch: set
Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:7 by GitHub <noreply@…>, 6 months ago

Resolution: fixed
Status: assignedclosed

In 751d732a:

Fixed #35056 -- Fixed system check crash on reverse m2m relations with related_name in ModelAdmin.filter_horizontal/vertical.

Thanks Thomas Feldmann for the report.

Regression in 107865780aa44914e21d27fdf4ca269bc61c7f01.

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 6 months ago

In 9a182f3d:

[5.0.x] Fixed #35056 -- Fixed system check crash on reverse m2m relations with related_name in ModelAdmin.filter_horizontal/vertical.

Thanks Thomas Feldmann for the report.

Regression in 107865780aa44914e21d27fdf4ca269bc61c7f01.
Backport of 751d732a3815a68bdb5b7aceda0e7d5981362c4a from main

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