Skip to main content

Plugs google maps V3 api into Django admin.

Project description

django-google-maps is a simple application that provides the basic hooks into google maps V3 api for use in Django models from Django version 1.11+.

Starting with django-google-maps version (0.7.0), Django 1.11+ is required because Django changed their widget template rendering system. Version 0.8.0 supports Django 2.0+, and as such removes support for Python 2.7

I’m using this to allow someone from the admin panels to type a freeform address, have the address geocoded on change and plotted on the map. If the location is not 100% correct, the user can drag the marker to the correct spot and the geo coordinates will update.

USAGE:

  • include the django_google_maps app in your settings.py

  • Add your Google Maps API Key in your settings.py as GOOGLE_MAPS_API_KEY

  • create a model that has both an address field and geolocation field

    from django.db import models
    from django_google_maps import fields as map_fields
    
    class Rental(models.Model):
        address = map_fields.AddressField(max_length=200)
        geolocation = map_fields.GeoLocationField(max_length=100)
  • in the admin.py include the following as a formfield_override

    from django.contrib import admin
    from django_google_maps import widgets as map_widgets
    from django_google_maps import fields as map_fields
    
    class RentalAdmin(admin.ModelAdmin):
        formfield_overrides = {
            map_fields.AddressField: {'widget': map_widgets.GoogleMapsAddressWidget},
        }
  • To change the map type (hybrid by default), you can add an html attribute on the AddressField widget. The list of allowed values is: hybrid, roadmap, satellite, terrain

    from django.contrib import admin
    from django_google_maps import widgets as map_widgets
    from django_google_maps import fields as map_fields
    
    class RentalAdmin(admin.ModelAdmin):
        formfield_overrides = {
            map_fields.AddressField: {
              'widget': map_widgets.GoogleMapsAddressWidget(attrs={'data-map-type': 'roadmap'})},
        }
  • To change the autocomplete options, you can add an html attribute on the AddressField widget. See https://developers.google.com/maps/documentation/javascript/places-autocomplete#add_autocomplete for a list of available options

    import json from django.contrib import admin
    from django_google_maps import widgets as map_widgets
    from django_google_maps import fields as map_fields
    
    class RentalAdmin(admin.ModelAdmin): formfield_overrides = {
        map_fields.AddressField: { 'widget':
        map_widgets.GoogleMapsAddressWidget(attrs={
          'data-autocomplete-options': json.dumps({ 'types': ['geocode',
          'establishment'], 'componentRestrictions': {
                      'country': 'us'
                  }
              })
          })
        },
    }

That should be all you need to get started.

I also like to make the geolocation field readonly in the admin so a user (myself) doesn’t accidentally change it to a nonsensical value. There is validation on the field so you can’t enter an incorrect value, but you could enter something that is not even close to the address you intended.

When you’re displaying the address back to the user, just request the map using the geocoordinates that were saved in your model. Maybe sometime when I get around to it I’ll see if I can create a method that will build that into the model.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_google_maps-0.14.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_google_maps-0.14.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file django_google_maps-0.14.0.tar.gz.

File metadata

  • Download URL: django_google_maps-0.14.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_google_maps-0.14.0.tar.gz
Algorithm Hash digest
SHA256 42e32703a7f5736f049d2e33f7fab05ac12cc73d959bc305cda81bc37011610d
MD5 8ea3ef6ff61c746ad20236d2eaba0f04
BLAKE2b-256 7952ccb272ea130599f5e65a6b998c7570754a89c77881d82dfea475fdf576b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_google_maps-0.14.0.tar.gz:

Publisher: python-publish.yml on madisona/django-google-maps

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_google_maps-0.14.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_google_maps-0.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 14f959f31e9a10bb16d17b62f500ea265a1d8b8f883b8919f809a080a7784681
MD5 2a5bc97f06152fc070fc8d293b11dba1
BLAKE2b-256 c0eb9d0647a3aeebbafef99287863abbbd884652feca4540ecb26a69410fa5f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_google_maps-0.14.0-py3-none-any.whl:

Publisher: python-publish.yml on madisona/django-google-maps

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page