Django Google Places is a Django plug-in, that helps cache responses from GoogleMaps.
This feature helps to reduce the number of requests for third-party resources, which will speed up the performance of your application.
Django plug-in for address auto-fill in Google Places.
- Python 3.7 or later.
- Django 2.2 or later.
- googlemaps 4.2.0.
This library is available in PyPI. It is advisable to use pip
during installation:
pip install django-google-places
To run a complete set of tests, do the following:
make test-all
-
Add
'places'
entries to Django’sINSTALLED_APPS
settings. -
Supports packages
modeltranslation
anddjango_countries
(optional). -
For each Google Maps web service request, API key or client identifier is required.
API keys can be created on 'APIs & Services' page at https://console.cloud.google.com/apis/credentials . More information on how to get started with Google Maps platform, and further information on creation/limitations of API key, see section Getting started with Google Maps Platform at https://developers.google.com/maps/gmp-get-started. -
Add your Google Maps API key to your
settings.ru
asGOOGLE_MAPS_API_KEY
and caching time asCACHING_TIME
. Also addread.meGOOGLE_PLACES_WRAPPER_CACHE_NAME
key for caching. By default, 'default' is used. Other settings are optional – see Google Maps instructions.
GOOGLE_PLACES_API_KEY="AIzaDummyKey",
CACHING_TIME=60 * 60 * 24,
MODELTRANSLATION_LANGUAGES=("en", "ru", "es"),
TIME_ZONE="UTC",
USE_TZ=True,
USE_I18N=True,
- Run migrations to upload models to your database:
python manage.py migrate places
- Google Maps responses can be saved in model
Place
whose manager is written for Google Maps response for "types" key, and fills the following related models:
AdministrativeAreaLevel1,
AdministrativeAreaLevel2,
AdministrativeAreaLevel3,
AdministrativeAreaLevel4,
AdministrativeAreaLevel5,
Locality,
SubLocalityLevel1,
SubLocalityLevel2,
SubLocalityLevel3,
SubLocalityLevel4,
SubLocalityLevel5,
Neighborhood,
Route
Django-google-places
supports all methodsgoogle-maps-services-python
https://github.com/googlemaps/google-maps-services-python Example of using django-google-places caching decorator:
from datetime import datetime
from places.clients import cacheable_gmaps
# Geocoding an address
geocode_result = cacheable_gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
# Look up an address with reverse geocoding
reverse_geocode_result = cacheable_gmaps.reverse_geocode((40.714224, -73.961452))
# Request directions via public transit
now = datetime.now()
directions_result = cacheable_gmaps.directions("Sydney Town Hall",
"Parramatta, NSW",
mode="transit",
departure_time=now)