Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to model a postal address

I need to model a postal address that can have multiline street address, the city, the state (province), and the postal code. the country is omitted.

I need to preserve line breaks in the street addresses but still be able to search the addresses.

I see two ways to do it:

class Address(models.Model):     street = models.ForeignKey('StreetAddress')     city = models.TextField()     province = models.TextField()     code = models.TextField()<br> class StreetAddress(models.Model):     line_number = models.IntegerField()     text = models.TextField() 

or this one which stores the street address in a single text field but uses special separator characters to encode line breaks:

class Address(models.Model):     street = models.TextField()     city = models.TextField()     province = models.TextField()     code = models.TextField() 

what is the best way to do it in terms of code readability and efficiency (or their balance)?

like image 535
akonsu Avatar asked Oct 08 '11 23:10

akonsu


People also ask

What is an example of a postal address?

Example (in the U.S.): Mr John Smith. 132, My Street, Kingston, New York 12401.


1 Answers

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") 
like image 90
Issac Kelly Avatar answered Oct 08 '22 12:10

Issac Kelly