Repository: django-extensions/django-extensions

Issue:
Use the ForeignKeyAutocompleteAdmin form as in a InlineModelAdmin
Hi,

It seems that there is no way to use the ForeignKeyAutocompleteAdmin form as in a InlineModelAdmin form.

I wanted to display a foreign key in a TabularInline form in my admin, but it seems impossible. That would be an awesome feature to add.

Regards,
Thibault



Discussion:
Someone seems to have developed a working solution. See here :

http://www.michelepasin.org/techblog/2009/10/12/autocomplete-in-django-2/

@thibault sorry for the delay, the link is broken, do you have more information about how we can fix this?

Hi,

Since this issue was opened 3 years ago, I'm not sure it's stille relevant ;)

Anyway, I think the old url has moved here:

http://www.michelepasin.org/blog/2009/10/12/autocomplete-in-django-2/

I'll leave it here as an improvement, if more people like the idea I'll work on it.

I'm closing this for now, if someone else is interested can reopen this issue.

It would be of great help for me to have this functionality developed

+1

I got a basic version working of ForeignKeyAutocompleteTabularInline working by simply copying 3 fields from ForeignKeyAutocompleteAdmin: _related_search_fields_, _get_help_text_, and _formfield_for_dbfield_. It's missing the search input, but it's good enough for me at the time being. Here's what I have so far:

```
import six
from django.contrib import admin
from django.db import models
from django.utils.text import get_text_list
from django_extensions.admin import ForeignKeySearchInput

from django.utils.translation import ugettext as _


class ForeignKeyAutocompleteTabularInline(admin.TabularInline):
    related_search_fields = {}

    def get_help_text(self, field_name, model_name):
        searchable_fields = self.related_search_fields.get(field_name, None)
        if searchable_fields:
            help_kwargs = {
                'model_name': model_name,
                'field_list': get_text_list(searchable_fields, _('and')),
            }
            return _('Use the left field to do %(model_name)s lookups in the fields %(field_list)s.') % help_kwargs
        return ''

    def formfield_for_dbfield(self, db_field, **kwargs):
        """
        Overrides the default widget for Foreignkey fields if they are
        specified in the related_search_fields class attribute.
        """
        if isinstance(db_field, models.ForeignKey) and db_field.name in self.related_search_fields:
            model_name = db_field.rel.to._meta.object_name
            help_text = self.get_help_text(db_field.name, model_name)
            if kwargs.get('help_text'):
                help_text = six.u('%s %s' % (kwargs['help_text'], help_text))
            kwargs['widget'] = ForeignKeySearchInput(db_field.rel, self.related_search_fields[db_field.name])
            kwargs['help_text'] = help_text
        return super(ForeignKeyAutocompleteTabularInline, self).formfield_for_dbfield(db_field, **kwargs)

```

@jpnauta could you please elaborate a PR with it?


Resolving patch:
diff --git a/django_extensions/admin/__init__.py b/django_extensions/admin/__init__.py
--- a/django_extensions/admin/__init__.py
+++ b/django_extensions/admin/__init__.py
@@ -15,12 +15,12 @@
 from django.utils.encoding import smart_str
 from django.utils.translation import ugettext as _
 from django.utils.text import get_text_list
-from django.contrib.admin import ModelAdmin
+from django.contrib import admin
 
 from django_extensions.admin.widgets import ForeignKeySearchInput
 
 
-class ForeignKeyAutocompleteAdmin(ModelAdmin):
+class ForeignKeyAutocompleteAdminMixin(object):
     """Admin class for models using the autocomplete feature.
 
     There are two additional fields:
@@ -60,7 +60,7 @@ def wrapper(*args, **kwargs):
         return [
             url(r'foreignkey_autocomplete/$', wrap(self.foreignkey_autocomplete),
                 name='%s_%s_autocomplete' % (self.model._meta.app_label, self.model._meta.model_name))
-        ] + super(ForeignKeyAutocompleteAdmin, self).get_urls()
+        ] + super(ForeignKeyAutocompleteAdminMixin, self).get_urls()
 
     def foreignkey_autocomplete(self, request):
         """
@@ -151,4 +151,16 @@ def formfield_for_dbfield(self, db_field, **kwargs):
                 help_text = six.u('%s %s' % (kwargs['help_text'], help_text))
             kwargs['widget'] = ForeignKeySearchInput(db_field.rel, self.related_search_fields[db_field.name])
             kwargs['help_text'] = help_text
-        return super(ForeignKeyAutocompleteAdmin, self).formfield_for_dbfield(db_field, **kwargs)
+        return super(ForeignKeyAutocompleteAdminMixin, self).formfield_for_dbfield(db_field, **kwargs)
+
+
+class ForeignKeyAutocompleteAdmin(ForeignKeyAutocompleteAdminMixin, admin.ModelAdmin):
+    pass
+
+
+class ForeignKeyAutocompleteTabularInline(ForeignKeyAutocompleteAdminMixin, admin.TabularInline):
+    pass
+
+
+class ForeignKeyAutocompleteStackedInline(ForeignKeyAutocompleteAdminMixin, admin.StackedInline):
+    pass


Test patch: