如何建立邮政地址 [英] how to model a postal address

查看:207
本文介绍了如何建立邮政地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个可以拥有多行街道地址,城市,州(省)和邮政编码的邮政地址。该国家将被省略。

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

推荐答案

除非你的大多数地址都有多行街道部分(并且有很多行),我会去后者,存储所有在一个单一的领域,而不是打扰一个额外的模型。如果大多数多行地址只有两行,请考虑在地址模型中创建一个街道和street2字段(您可以为这两个街道字段选择更多描述性名称)。第一个将存储第一条街道线,第二个字段将存储所有附加行(用换行符分隔)。我会在搜索地址时考虑,您最经常搜索包含街道号码的地址行,所以也许在您的程序逻辑中,您可以确保街道号码线始终存储在第一个街道字段中,然后,您可以在数据库中添加一个索引。

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.

这篇关于如何建立邮政地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆