diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 48b93166c8e..e67f0815577 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -215,9 +215,10 @@ class BaseModelAdmin(six.with_metaclass(RenameBaseModelAdminMethods)): }) kwargs['empty_label'] = _('None') if db_field.blank else None - queryset = self.get_field_queryset(db, db_field, request) - if queryset is not None: - kwargs['queryset'] = queryset + if not 'queryset' in kwargs: + queryset = self.get_field_queryset(db, db_field, request) + if queryset is not None: + kwargs['queryset'] = queryset return db_field.formfield(**kwargs) @@ -238,9 +239,10 @@ class BaseModelAdmin(six.with_metaclass(RenameBaseModelAdminMethods)): elif db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)): kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical)) - queryset = self.get_field_queryset(db, db_field, request) - if queryset is not None: - kwargs['queryset'] = queryset + if not 'queryset' in kwargs: + queryset = self.get_field_queryset(db, db_field, request) + if queryset is not None: + kwargs['queryset'] = queryset return db_field.formfield(**kwargs) diff --git a/docs/releases/1.6.1.txt b/docs/releases/1.6.1.txt index d7f73903ff2..db05bcdca26 100644 --- a/docs/releases/1.6.1.txt +++ b/docs/releases/1.6.1.txt @@ -28,3 +28,5 @@ Bug fixes non related to file extensions (#21457). * Fixed display of inline instances in formsets when parent has 0 for primary key (#21472). +* Fixed a regression where custom querysets for foreign keys were overwritten + if ``ModelAdmin`` had ordering set (#21405). diff --git a/tests/admin_ordering/tests.py b/tests/admin_ordering/tests.py index d4252d5036a..0d62951e754 100644 --- a/tests/admin_ordering/tests.py +++ b/tests/admin_ordering/tests.py @@ -147,3 +147,25 @@ class TestRelatedFieldsAdminOrdering(TestCase): # should be ordered by rank (defined by the ModelAdmin) self.check_ordering_of_field_choices([self.b1, self.b2]) + + def test_custom_queryset_still_wins(self): + """Test that custom queryset has still precedence (#21405)""" + class SongAdmin(admin.ModelAdmin): + # Exclude one of the two Bands from the querysets + def formfield_for_foreignkey(self, db_field, **kwargs): + if db_field.name == 'band': + kwargs["queryset"] = Band.objects.filter(rank__gt=2) + return super(SongAdmin, self).formfield_for_foreignkey(db_field, **kwargs) + def formfield_for_manytomany(self, db_field, **kwargs): + if db_field.name == 'other_interpreters': + kwargs["queryset"] = Band.objects.filter(rank__gt=2) + return super(SongAdmin, self).formfield_for_foreignkey(db_field, **kwargs) + + class StaticOrderingBandAdmin(admin.ModelAdmin): + ordering = ('rank',) + + admin.site.unregister(Song) + admin.site.register(Song, SongAdmin) + admin.site.register(Band, StaticOrderingBandAdmin) + + self.check_ordering_of_field_choices([self.b2])