2

ModelAdmin.autocomplete_fields looks to be very simple to implement into the Django admin:

class UserAdmin(admin.ModelAdmin):
    autocomplete_fields = ['material']
admin.site.register(User, UserAdmin)    

class MaterialAdmin(admin.ModelAdmin):
        search_fields = ['name']
admin.site.register(Material, MaterialAdmin)

It renders the field correctly (as a search field instead of a dropdown), but the search field says "The results could not be loaded" and inspect shows:

*/admin/autocomplete/ 403 (Forbidden) jquery.js:9203

I assume there is a csrf issue receiving data from the Material model. I looked into ways to exempt this request from csrf but couldn't figure out how to do it through ModelAdmin.autocomplete_fields.

I tried using django-autocomplete-light as well and couldn't get it working.

Peter
  • 21
  • 3
  • probably, you may not have sufficient permissions [Ref this](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields) – JPG Jul 22 '21 at 17:17

3 Answers3

1

If you implement your own UserAdmin either make sure search_fields is defined, as requested in the documentation, or use Django's UserAdmin as a base class.

Also, if you just upgraded your Django version and it stopped working, clear your cache to load the proper javascript files.

Manel Clos
  • 1,785
  • 2
  • 19
  • 15
1

That's because of the conflict between DAL and Django 3.2 + versions. If you turn DAL off it can solve this problem. DAL's js overloads Django's one and that's it. To know more just follow the link to Dal's github issue

0

I've had the same problem, and it was due to the django version, which had the project in a previous version and when I passed it to the newer one, it began to give me this type of error. I recommend that you try to comment the lines that contain autocomplete_fields, since django 3.2 already does it automatically.

Greetings

class ArticulosAdmin(admin.ModelAdmin):

form = ArticulosAdminForm
list_display = ['Articulo', 'Autores','tipoDoc','Etiqueta']
search_fields = ['nomArt','nomOrigDoc','etiDoc','anoArt','anoOrigDoc']
filter_horizontal = ['materia','palabraClave','idioma']
fields = ('materia','palabraClave','nomArt','nomOrigDoc','revista','pagArt','anoArt','anoOrigDoc','resumenEsDoc','resumenEnDoc','resumenFrDoc','consigDoc','idioma','stockDoc','printDoc','derechoDoc')
# autocomplete_fields = ['revista']
inlines = [ArticuloAutorInline]
change_form_template = 'admin/repositorio/change_form.html'

Once you comment those lines, you will see that your problem will be solved.

Source:

https://groups.google.com/g/django-users/c/RQ-dFgcBaIQ/m/RX5kFxbOAgAJ?pli=1+ https://github.com/sehmaschine/django-grappelli/issues/963

MPerez
  • 1
  • 3