Repository: django-extensions/django-extensions

Issue:
2.0.8 breaks ForeignKeyAutocompleteAdmin
I have a model (SocialMediaIdentity) with ForeignKeyAutocompleteAdmin handling the "user" field, which is a ForeignKey to User:

    related_search_fields = {
        'user': ('username', 'email',),
    }

Upgrading from 2.0.7 to 2.0.8 breaks this. Digging through I find that the issue is that model_name has the value "socialmediaidentity" (i.e. the model for which I'm using ForeignKeyAutocompleteAdmin), whereas it should be "user". Thus it looks for username__icontains in the SocialMediaIdentity model instead of in the User model.



Discussion:
Would it be possible to come up with a PR for a test for this ?
I meant django-extensions 2.0.8, not Django 2.0.8


On Aug 04, 2018, at 08:23 PM, Tim Graham <notifications@github.com> wrote:

I think there's a mistake in your report as I don't see anything in the 2.0.8 release notes that looks like a relevant change. Also, you filed the issue before 2.0.8 was released (3 days ago).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Resolving patch:
diff --git a/django_extensions/admin/widgets.py b/django_extensions/admin/widgets.py
--- a/django_extensions/admin/widgets.py
+++ b/django_extensions/admin/widgets.py
@@ -35,7 +35,7 @@ def _media(self):
 
     def label_for_value(self, value):
         key = self.rel.get_related_field().name
-        obj = self.rel.related_model._default_manager.get(**{key: value})
+        obj = self.rel.model._default_manager.get(**{key: value})
 
         return Truncator(obj).words(14, truncate='...')
 
@@ -46,7 +46,7 @@ def __init__(self, rel, search_fields, attrs=None):
     def render(self, name, value, attrs=None):
         if attrs is None:
             attrs = {}
-        opts = self.rel.related_model._meta
+        opts = self.rel.model._meta
         app_label = opts.app_label
         model_name = opts.object_name.lower()
         related_url = reverse('admin:%s_%s_changelist' % (app_label, model_name))


Test patch:
diff --git a/tests/test_admin_widgets.py b/tests/test_admin_widgets.py
new file mode 100644
--- /dev/null
+++ b/tests/test_admin_widgets.py
@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+from django.test import TestCase
+from django.utils.text import Truncator
+
+from django_extensions.admin import widgets
+
+from .testapp import models
+
+
+class ForeignKeySearchInputTestCase(TestCase):
+
+    def test_widget_works(self):
+        name = models.Name.objects.create(name="Name")
+        person = models.Person.objects.create(
+            name=name,
+            age=30,
+        )
+        club = models.Club.objects.create(
+            name='Club',
+        )
+        membership = models.Membership.objects.create(club=club, person=person)
+
+        widget = widgets.ForeignKeySearchInput(
+            models.Membership._meta.get_field('person').remote_field,
+            ['person__name'])
+
+        label = widget.label_for_value(membership.pk)
+
+        self.assertEqual(
+            Truncator(person).words(14, truncate='...'),
+            label)
+
+        # Just making sure rendering the widget doesn't cause any issue
+        widget.render('person', person.pk)
+        widget.render('person', None)
diff --git a/tests/testapp/admin.py b/tests/testapp/admin.py
new file mode 100644
--- /dev/null
+++ b/tests/testapp/admin.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+from django.contrib import admin
+
+from . import models
+
+admin.site.register(models.Person)