diff options
| author | Hrushikesh <hrushikeshrv@gmail.com> | 2023-06-16 19:06:24 +0530 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-06-21 07:48:09 +0200 |
| commit | 107865780aa44914e21d27fdf4ca269bc61c7f01 (patch) | |
| tree | bb8163452630f2cbd4322400aaa8cdab4a6d57f2 /tests/modeladmin | |
| parent | ddb6506618ea52c6b20e97eefad03ed847a1e3de (diff) | |
Fixed #34345 -- Added system check for ManyToManyFields with intermediate tables in ModelAdmin.filter_horizontal/vertical.
Diffstat (limited to 'tests/modeladmin')
| -rw-r--r-- | tests/modeladmin/test_checks.py | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/tests/modeladmin/test_checks.py b/tests/modeladmin/test_checks.py index 69aa242a64..2ed27f8a3d 100644 --- a/tests/modeladmin/test_checks.py +++ b/tests/modeladmin/test_checks.py @@ -4,10 +4,11 @@ from django.contrib.admin import BooleanFieldListFilter, SimpleListFilter from django.contrib.admin.options import VERTICAL, ModelAdmin, TabularInline from django.contrib.admin.sites import AdminSite from django.core.checks import Error -from django.db.models import CASCADE, F, Field, ForeignKey, Model +from django.db.models import CASCADE, F, Field, ForeignKey, ManyToManyField, Model from django.db.models.functions import Upper from django.forms.models import BaseModelFormSet from django.test import SimpleTestCase +from django.test.utils import isolate_apps from .models import Band, Song, User, ValidationTestInlineModel, ValidationTestModel @@ -321,6 +322,26 @@ class FilterVerticalCheckTests(CheckTestCase): "admin.E020", ) + @isolate_apps("modeladmin") + def test_invalid_m2m_field_with_through(self): + class Artist(Model): + bands = ManyToManyField("Band", through="BandArtist") + + class BandArtist(Model): + artist = ForeignKey("Artist", on_delete=CASCADE) + band = ForeignKey("Band", on_delete=CASCADE) + + class TestModelAdmin(ModelAdmin): + filter_vertical = ["bands"] + + self.assertIsInvalid( + TestModelAdmin, + Artist, + "The value of 'filter_vertical[0]' cannot include the ManyToManyField " + "'bands', because that field manually specifies a relationship model.", + "admin.E013", + ) + def test_valid_case(self): class TestModelAdmin(ModelAdmin): filter_vertical = ("users",) @@ -363,6 +384,26 @@ class FilterHorizontalCheckTests(CheckTestCase): "admin.E020", ) + @isolate_apps("modeladmin") + def test_invalid_m2m_field_with_through(self): + class Artist(Model): + bands = ManyToManyField("Band", through="BandArtist") + + class BandArtist(Model): + artist = ForeignKey("Artist", on_delete=CASCADE) + band = ForeignKey("Band", on_delete=CASCADE) + + class TestModelAdmin(ModelAdmin): + filter_horizontal = ["bands"] + + self.assertIsInvalid( + TestModelAdmin, + Artist, + "The value of 'filter_horizontal[0]' cannot include the ManyToManyField " + "'bands', because that field manually specifies a relationship model.", + "admin.E013", + ) + def test_valid_case(self): class TestModelAdmin(ModelAdmin): filter_horizontal = ("users",) |
