Skip to main content

A django application for describing addresses.

Project description

Django Address

Django models for storing and retrieving postal addresses.


Overview

Django Address is a set of models and methods for working with postal addresses.

Requirements

  • Python (3.5, 3.6, 3.7, 3.8)
  • Django (2.2, 3.0)

We recommend and only officially support the latest patch release of each Python and Django series.

Installation

For more detailed instructions, view the Readme for the example site included with this package.

pip install django-address

Then, add address to your INSTALLED_APPS list in settings.py:

INSTALLED_APPS = [
    ... 
    'django_filters',
]

You wil need to add your Google Maps API key to settings.py too:

GOOGLE_API_KEY = 'AIzaSyD--your-google-maps-key-SjQBE'

The Model

The rationale behind the model structure is centered on trying to make it easy to enter addresses that may be poorly defined. The model field included uses Google Maps API v3 (via the nicely done geocomplete jquery plugin) to determine a proper address where possible. However if this isn't possible the raw address is used and the user is responsible for breaking the address down into components.

It's currently assumed any address is represent-able using four components: country, state, locality and street address. In addition, country code, state code and postal code may be stored, if they exist.

There are four Django models used:

  Country
    name
    code

  State
    name
    code
    country -> Country

  Locality
    name
    postal_code
    state -> State

  Address
    raw
    street_number
    route
    locality -> Locality

Address Field

To simplify storage and access of addresses, a subclass of ForeignKey named AddressField has been created. It provides an easy method for setting new addresses.

Creation

It can be created using the same optional arguments as a ForeignKey field. For example:

  from address.models import AddressField

  class MyModel(models.Model):
    address1 = AddressField()
    address2 = AddressField(related_name='+', blank=True, null=True)

Setting Values

Values can be set either by assigning an Address object:

  addr = Address(...)
  addr.save()
  obj.address = addr

Or by supplying a dictionary of address components:

  obj.address = {'street_number': '1', 'route': 'Somewhere Ave', ...}

The structure of the address components is as follows:

  {
    'raw': '1 Somewhere Ave, Northcote, VIC 3070, AU',
    'street_number': '1',
    'route': 'Somewhere Ave',
    'locality': 'Northcote',
    'postal_code': '3070',
    'state': 'Victoria',
    'state_code': 'VIC',
    'country': 'Australia',
    'country_code': 'AU'
  }

All except the raw field can be omitted. In addition, a raw address may be set directly:

obj.address = 'Out the back of 1 Somewhere Ave, Northcote, Australia'

Getting Values

When accessed, the address field simply returns an Address object. This way all components may be accessed naturally through the object. For example::

  route = obj.address.route
  state_name = obj.address.locality.state.name

Forms

Included is a form field for simplifying address entry. A Google maps auto-complete is performed in the browser and passed to the view. If the lookup fails the raw entered value is used.

TODO: Talk about this more.

Partial Example

The model:

from address.models import AddressField

class Person(models.Model):
  address = AddressField(on_delete=models.CASCADE)

The form:

from address.forms import AddressField

class PersonForm(forms.Form):
  address = AddressField()

The template:

<head>
{{ form.media }} <!-- needed for JS/GoogleMaps lookup -->
</head>
<body>
  {{ form }}
</body>

Project Status Notes

This library was created by Luke Hodkinson originally focused on Australian addresses.

In 2015 Luke began working to abstract the project so it could handle a wider variety of international addresses.

This became the current dev branch. While good progress was made on this, the branch became stale and releases continued under the current model architecture on master.

The project is currently in triage, for releases 0.2.2 and 0.2.3, with a both a model re-architecture and updated requirements for 0.3.0. Read more about the project path forward in this issue.

If you have questions, bug reports or suggestions please create a New Issue for the project.

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-address-0.2.2.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

django_address-0.2.2-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

File details

Details for the file django-address-0.2.2.tar.gz.

File metadata

  • Download URL: django-address-0.2.2.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for django-address-0.2.2.tar.gz
Algorithm Hash digest
SHA256 45eef8614a9009a40ae347c8ad75b34492520a9d3978b88e23f85f578433a41b
MD5 e50d97dea39a4c2d6cc19d386e96c3a6
BLAKE2b-256 8c20017f18793a98d4355e6a7c1b636e6933760dabbd742359529150da1a9eed

See more details on using hashes here.

File details

Details for the file django_address-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: django_address-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 28.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for django_address-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4fa67b4455adad6c86802165abb64bf472e4a44b670107aba8de3247eebf8e70
MD5 0a81b090fb96d72f35da29aab6013f35
BLAKE2b-256 a4963c887d75bb85b5fefd6df514de10ec6420362e7435a592fa3e5155531d37

See more details on using hashes here.

Supported by

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