English 中文(简体)
如何模仿邮政地址
原标题: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.

我需要保持街道地址的线性断裂,但仍能够搜索这些地址。

我看到这样做的两种方式:

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()

或者在单一文本领域储存街道地址但使用特殊分离特性打上条码线的照片:

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

在法典的可读性和效率(或两者的平衡)方面,这样做的最佳方式是什么?

最佳回答

除非你的大多数地址有多线街道部分(而且有许多条线),否则我就跑到后者,将其存放在一个单一的领域,而不是同时使用另一个模式。 如果你的大多数多线地址只有两条线,考虑在你的地址模式中建立一个街道和街道2(你可以为这两个“秘密”领域选择更多的描述性名称)。 第一个将储存第一个街道线,第二个外地将储存所有新增线路(由新线连接)。 我想,在寻找地址时,你常常在包含街道号码的地址线上搜寻,因此,或许按照你的方案逻辑,你可以确保街道号码线总是储存在第一个“秘密”领域,然后可以在你的数据库中添加一个索引。

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.

如果你事先不了解,今后不考虑可能“移徙”的倾向,就采用更简单的模式。 否则,就算作你的双轨设计。

问题回答

这里我的模式如何针对美国。 如果需要的话,你还可以储存10位数字代码(XXXXX-XXXX)。

您也可以考虑根据您对地址的重新使用情况,在地德詹戈增设一个点或一个多领域。

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")

我对你的地址数据有一些问题:

  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)?

大多数交付地址只有一个线。 许多人错误地将二级信息(unit number)放在第二交付线上。 这违反了美国卫生和植物检疫标准(见。 因此,我建议你通过CASS-Certised获取你的地址数据。 请你知道,你所处理的地址是真实的,是标准化的。 然后,你很可能能够更好地分析你的数据,并就如何储存数据作出决定。 我的猜测是,在你的数据库上进行核查后,你打得的塔克有太多(如果有的话)的地址,不止一个交付线。 届时,您必须决定是否值得为之单独设一个领域,或将其保留在一个有条不紊的单一领域。

全面披露: http://www.smartystreets.com

查阅django-address

它提供了一个示范领域,使你可以采取以下行动:

from address.models import AddressField

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




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签