furious-luke / django-address

A Django address model and field. Addresses may be specified by address components or by performing an automatic Google Maps lookup.
BSD 3-Clause "New" or "Revised" License
428 stars 178 forks source link

Error when using together with Django autocomplete fields in admin #180

Open spechtx opened 1 year ago

spechtx commented 1 year ago

I'm registering MyModel in the admin:

@admin.register(MyModel)
class MyModel(admin.ModelAdmin):
    search_fields = ['field_1',]
    filter_horizontal = ['phone_fk', 'email_fk',]
    formfield_overrides = {AddressField: {"widget": AddressWidget(attrs={"style": "width: 400px;"})}}
    autocomplete_fields = ['parent', ]

When autocomplete_fields is set, following error occurs: jquery.js:4050 jQuery.Deferred exception: self.geocomplete is not a function TypeError: self.geocomplete is not a function at HTMLInputElement. (http://127.0.0.1:8081/static/address/js/address.js:6:8) at Function.each (http://127.0.0.1:8081/static/admin/js/vendor/jquery/jquery.js:385:19) at jQuery.fn.init.each (http://127.0.0.1:8081/static/admin/js/vendor/jquery/jquery.js:207:17) at HTMLDocument. (http://127.0.0.1:8081/static/address/js/address.js:2:21) at mightThrow (http://127.0.0.1:8081/static/admin/js/vendor/jquery/jquery.js:3766:29) at process (http://127.0.0.1:8081/static/admin/js/vendor/jquery/jquery.js:3834:12) undefined jQuery.Deferred.exceptionHook @ jquery.js:4050 process @ jquery.js:3838 setTimeout (async) (anonymous) @ jquery.js:3872 fire @ jquery.js:3500 fireWith @ jquery.js:3630 fire @ jquery.js:3638 fire @ jquery.js:3500 fireWith @ jquery.js:3630 ready @ jquery.js:4110 completed @ jquery.js:4120 jquery.js:4059 Uncaught TypeError: self.geocomplete is not a function at HTMLInputElement. (address.js:6:8) at Function.each (jquery.js:385:19) at jQuery.fn.init.each (jquery.js:207:17) at HTMLDocument. (address.js:2:21) at mightThrow (jquery.js:3766:29) at process (jquery.js:3834:12)

My guess is, the autocomplete field loads it's on jQuery for select2, and this somehow interferes...

Any idea for a workaround?

benoitperrin commented 11 months ago

@spechtx Thanks for pointing this out. I am running into the same issue. Have you found a workaround in your case?

banagale commented 11 months ago

There's a warning about autocomplete_fields implying custom widgets of their own in the django docs here: https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides

benoitperrin commented 10 months ago

I was not able to find a link with the issue mentioned here. My “workaround” is not to use autocomplete_fields and AddressWidget on the same page.

IMHO, a solution here lies in the deprecation of the use of jquery in this library, as described in https://github.com/furious-luke/django-address/issues/139.