4

I'm trying to use autocomplete-fields in TabularInine

Below my code :

class PersonInstitutionsInline(admin.TabularInline):
    autocomplete_fields = ['institution']
    model = PersonInstitution
    extra = 0

When rendering the dropdown is simply empty with no error (javacsript or python)

If I use the same with StackedInline it works correctly

While checking the templates, I'm suspecting that it has to with the fact that stacked.html uses {% include "admin/includes/fieldset.html" %} but tabular.html uses {{ field.field }}

Also on another note, if I try to use django-autocomplete-light form in any inline

inlines.js:20 Uncaught TypeError: Cannot read property 'fn' of undefined
    at inlines.js:20
    at inlines.js:298

with in python the error

MediaOrderConflictWarning: Detected duplicate Media files in an opposite order:
admin/js/autocomplete.js
admin/js/admin/DateTimeShortcuts.js
  MediaOrderConflictWarning,

I'm using Django version 2.1.7

Anyone had a similar issue or is there a fix for it ?

Currently the workaround I'm using is StackedInline, and putting them all in the same fieldset...but i think there should be a better solution for it.

Thanks

Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88
Dany Y
  • 6,833
  • 6
  • 46
  • 83

1 Answers1

0

You can use django-autocomplete-light lib https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html

from dal import autocomplete

from django import forms


class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        fields = ('__all__')
        widgets = {
            'birth_country': autocomplete.ModelSelect2(url='country-autocomplete')
        }

Using autocompletes in the admin We can make ModelAdmin to use our form, ie:

from django.contrib import admin

from your_person_app.models import Person
from your_person_app.forms import PersonForm


class PersonAdmin(admin.ModelAdmin):
    form = PersonForm

admin.site.register(Person, PersonAdmin)

Note that this also works with inlines, ie:

class PersonInline(admin.TabularInline):
    model = Person
    form = PersonForm
Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88