[Django]-How to model a postal address

6đź‘Ť

âś…

Unless the majority of your addresses have multi-line street parts (and have many lines), I’d go for the latter, storing it all in a single field, and not bothering with an additional model. If most of your multi-line addresses are only two lines, consider creating a street and street2 field in your Address model (you could choose more descriptive names for these two “street” fields). The first would store the first street line, and the second field would store all additional lines (separated by newlines). I would think when searching addresses, you’d most often search on the address line that contains the street number, so maybe in your program logic you’d ensure that the street number line was always stored in the first “street” field, which you could then add an index on in your database.

On the other hand, if most of your addresses will have multi-line street parts, and have more than two lines, then it makes sense to create that second model.

If you don’t know in advance, and don’t mind potentially “migrating” in the future, go for the simpler model. Otherwise, go for your two-model design.

👤imm

17đź‘Ť

Here’s how I model addresses for the US. You could also store a 10 digit zip code (XXXXX-XXXX) if you needed.

You may also consider adding a point field, or a poly field from geodjango depending on what you’re using the addresses for.

from django.contrib.gis.db import models
from django.utils.translation import ugettext as _
from django.contrib.localflavor.us.models import USStateField

class UsLocation(models.Model):
    address_1 = models.CharField(_("address"), max_length=128)
    address_2 = models.CharField(_("address cont'd"), max_length=128, blank=True)

    city = models.CharField(_("city"), max_length=64, default="Zanesville")
    state = USStateField(_("state"), default="OH")
    zip_code = models.CharField(_("zip code"), max_length=5, default="43701")
👤Issac Kelly

12đź‘Ť

I have some questions about your address data:

  1. Is the data already verified down to the delivery point?
  2. If so, do you have a high percentage that still have more than one delivery line (street field)?

Most delivery addresses only have a single line. Many people erroneously place secondary information (unit number) on the second delivery line. This goes against USPS standards (see USPS publication 28). So, I recommend that you get your address data verified using a CASS-Certified vender so you know that the addresses you are dealing with are real and standardized. Then you will probably be in a better position to analyze your data and make a decision about how to store it. My guess is that after performing address verification on your database you won’t have very many (if any) address with more than a single delivery line. You’d have to decide at that point whether it’s worth it to have a separate field for it or keep it in a single field that’s delimited with a line break.

Full disclosure: I’m a software developer for SmartyStreets, an address verification company and CASS-Certified vender.

0đź‘Ť

Check out django-address.

It provides a model field where you can do the following

from address.models import AddressField

class SuperAwesomeCustomer(models.Model):
    address = AddressField()
👤willyhakim

Leave a comment