0

I have a model which uses source and destination fields and these are usually populated from a frontend template. Template has two form fields which are associated with Google map autocomplete APIs. But if I want to create a new object through admin panel, how can I integrate autocomplete API with add form's source and destination fields?

pnhegde
  • 695
  • 1
  • 8
  • 19

2 Answers2

2

You can easily use something like https://github.com/ubilabs/geocomplete/ in django admin —

Override the change form template to include the required js:

{% load staticfiles %}
{% block extrahead %}{{ block.super }}
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/geocomplete/1.4/jquery.geocomplete.min.js"></script>

    <script src="{% static 'path/to/your/js/geocomplete.js' %}"></script>

{% endblock %}

In your own js file:

$("#address_input").geocomplete(); 

Docs here on how to populate form using the library:

https://github.com/ubilabs/geocomplete/#populate-form-data

mishbah
  • 5,487
  • 5
  • 25
  • 35
0

There is Django admin widget called django-location-field that does exactly what you ask. I use it in my projects.

https://github.com/caioariede/django-location-field

Sam Texas
  • 1,245
  • 14
  • 30