Login

Models for Postal Addresses

Author:
MasonM
Posted:
July 24, 2008
Language:
Python
Version:
.96
Score:
2 (after 6 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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.