django-admin-autocomplete-filter icon indicating copy to clipboard operation
django-admin-autocomplete-filter copied to clipboard

Fix many-to-many by finding related model from descriptor

Open bpartridge opened this issue 6 years ago • 3 comments

In https://github.com/farhan0581/django-admin-autocomplete-filter/blob/master/admin_auto_filters/filters.py#L53 we attempt to call .get_queryset() on the descriptor of the field. For foreign keys this works fine, but for ManyToManyDescriptor (which is present if you're referencing a ManyToManyField), Django never defines get_queryset on the class or its superclass: https://github.com/django/django/blob/master/django/db/models/fields/related_descriptors.py#L748

This is kind of an oversight and broken-duck-typing on the Django team's part, and it's trivially and harmlessly monkey-patched by doing the following somewhere, which causes the filter to work as intended.

ManyToManyDescriptor.get_queryset = lambda self: self.rel.model.objects.get_queryset()

But the correct fix would be to fall back to a call like this in the filter code itself - this PR does exactly that!

bpartridge avatar Feb 21 '19 05:02 bpartridge

Thanks buddy for your contribution. Please review your pull request once to resolve merge conflict and please raise the pull request to dev branch.

farhan0581 avatar Feb 27 '19 09:02 farhan0581

Will do once I look at @merwok's comment!

bpartridge avatar Feb 27 '19 16:02 bpartridge

I think we may have addressed this, both in 0.6.1 and with additional changes in pre_release.

jaredahern avatar Mar 01 '21 01:03 jaredahern