Login

Models for Postal Addresses

Author:
MasonM
Posted:
July 24, 2008
Language:
Python
Version:
.96
Tags:
models countries
Score:
1 (after 5 ratings)

Here's some fairly normalized models for representing postal addresses. Making postal_code a separate model would probably be the only way to get it to a level that everyone would agree is 2NF, but we handle a lot of international addresses so that isn't really feasible.

Country and StateProvince are intended to be populated with data from ISO 3166. I'd include the SQL I used with that data, but it's very long and there's no attach feature. Also, I'd probably be violating ISO's copyright.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Country(models.Model):
    """Model for countries"""
    iso_code = models.CharField(max_length = 2, primary_key = True)
    name = models.CharField(max_length = 45, blank = False)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Countries"
        ordering = ["name", "iso_code"]


class StateProvince(models.Model):
    """Model for states and provinces"""
    iso_code = models.CharField(max_length = 3, primary_key = True)
    name = models.CharField(max_length = 55, blank = False)
    country = models.ForeignKey(Country, to_field="iso_code")

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = "State or province"
        """the admin site dies when I try this. apparantely support for
           ordering by foreign keys is broken. uncomment when fixed
           ordering = ["-country", "name"]
        """

class Address(models.Model):
    """Model to store addresses for accounts"""
    address_line1 = models.CharField("Address line 1", max_length = 45)
    address_line2 = models.CharField("Address line 2", max_length = 45,
        blank = True)
    postal_code = models.CharField("Postal Code", max_length = 10)
    city = models.CharField(max_length = 50, blank = False)
    state_province = models.CharField("State/Province", max_length = 40,
        blank = True)
    country = models.ForeignKey(Country, blank = False)

    def __unicode__(self):
        return "%s, %s %s" % (self.city, self.state_province,
                              str(self.country))

    class Meta:
        verbose_name_plural = "Addresses"
        unique_together = ("address_line1", "address_line2", "postal_code",
                           "city", "state_province", "country")

More like this

  1. JSON fixtures of Intl. country codes & dial-codes by dibau_naum_h 4 years, 11 months ago
  2. CountryField (UN Country List, 3 Char Codes) by djm 6 years, 6 months ago
  3. CountryField (ISO 3166-1) by dougal 6 years, 3 months ago
  4. Retrieve a list of countries from GeoNames by simon 6 years, 10 months ago
  5. Save Geolocation for an Address based model on save() by publicFunction 2 years ago

Comments

Please login first before commenting.