存储街道地址和邮政编码
我正在尝试决定如何存储一些地址信息。这就是我的想法(它是 Django 模型形式,但你应该能够阅读它)
class City(models.Model):
name = models.CharField(max_length=100)
province = models.ForeignKey(Province)
data_source = models.PositiveIntegerField(default=DataSources.USER)
latitude = models.DecimalField(max_digits=18, decimal_places=12, null=True, blank=True)
longitude = models.DecimalField(max_digits=18, decimal_places=12, null=True, blank=True)
feature_class = models.CharField(max_length=1, null=True, blank=True)
feature_code = models.CharField(max_length=10, null=True, blank=True)
population = models.BigIntegerField(null=True, blank=True)
elevation = models.PositiveIntegerField(null=True, blank=True)
time_zone = models.CharField(max_length=40, null=True, blank=True)
mod_date = models.DateTimeField(auto_now=True, auto_now_add=True)
class Province(models.Model):
code = models.CharField(max_length=2, primary_key=True)
name = models.CharField(max_length=100)
country = models.ForeignKey(Country)
class Country(models.Model):
code = models.CharField(max_length=2, primary_key=True)
name = models.CharField(max_length=100)
class Address(models.Model):
name = models.CharField(max_length=100)
street = models.CharField(max_length=200)
postal_code = models.ForeignKey(PostalCode)
class PostalCode(models.Model):
code = models.CharField(max_length=10)
city = models.ForeignKey(City)
data_source = models.PositiveIntegerField(default=DataSources.USER)
latitude = models.DecimalField(max_digits=18, decimal_places=12, null=True, blank=True)
longitude = models.DecimalField(max_digits=18, decimal_places=12, null=True, blank=True)
我已经有了一个包含城市、省份、国家和邮政编码的数据库。在大多数情况下,用户只会输入地址。
这样,如果用户输入邮政编码,我可以自动为他填充城市、省份和国家/地区字段。但如果我的数据已过时或不正确怎么办?然后用户只需使用正确的信息更新这些字段,然后我就可以相应地更新我的数据库。
但是,如果他输入别人的邮政编码,然后将城市更新为不正确的内容,会发生什么情况呢?他刚刚泄露了别人的地址。
那么也许我应该将城市字段移至地址类?但后来我无法再通过邮政编码查找城市。
那么也许我应该在两个模型中都包含城市?但现在我有重复。这仍然是最好的选择吗?
I'm trying to decide how to store some address information. This is what I have in mind (it's in Django model form, but you should be able to read it)
class City(models.Model):
name = models.CharField(max_length=100)
province = models.ForeignKey(Province)
data_source = models.PositiveIntegerField(default=DataSources.USER)
latitude = models.DecimalField(max_digits=18, decimal_places=12, null=True, blank=True)
longitude = models.DecimalField(max_digits=18, decimal_places=12, null=True, blank=True)
feature_class = models.CharField(max_length=1, null=True, blank=True)
feature_code = models.CharField(max_length=10, null=True, blank=True)
population = models.BigIntegerField(null=True, blank=True)
elevation = models.PositiveIntegerField(null=True, blank=True)
time_zone = models.CharField(max_length=40, null=True, blank=True)
mod_date = models.DateTimeField(auto_now=True, auto_now_add=True)
class Province(models.Model):
code = models.CharField(max_length=2, primary_key=True)
name = models.CharField(max_length=100)
country = models.ForeignKey(Country)
class Country(models.Model):
code = models.CharField(max_length=2, primary_key=True)
name = models.CharField(max_length=100)
class Address(models.Model):
name = models.CharField(max_length=100)
street = models.CharField(max_length=200)
postal_code = models.ForeignKey(PostalCode)
class PostalCode(models.Model):
code = models.CharField(max_length=10)
city = models.ForeignKey(City)
data_source = models.PositiveIntegerField(default=DataSources.USER)
latitude = models.DecimalField(max_digits=18, decimal_places=12, null=True, blank=True)
longitude = models.DecimalField(max_digits=18, decimal_places=12, null=True, blank=True)
I've already got a database full of cities, provinces, countries, and postal codes. For the most part, users will only be entering addresses.
This way, if a user enters a postal code, I can populate the city, province and country fields automatically for him. But what if my data is out of date, or is incorrect? Then the user just has to update those fields with the correct information, and then I can update my database accordingly.
But what happens then if he enters someone else's postal code, and then updates the city to something incorrect? He's just broke someone else's address.
So maybe I should move the city field to the Address class instead? But then I can no longer look up the city from the postal code.
So then maybe I should just have cities in both models? But now I've got duplication. Is this still the best option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我保留设计,只是将唯一约束放在(邮政编码,城市)和(城市,省)上。这样,如果相同的条目溢出到另一个区域,它们可能会出现两次。
I'm keeping the design and just putting the unique constraint on (postal_code,city), and (city,province) instead. That way the same entries can appear twice if they spill over to another region.
并不真地。您正在混淆两个不同的功能方面。
标准化您的数据,以免出现重复。否则你的数据库就会瘫痪。
安全。为什么你会允许任何人更新参考表(构成地址的静态数据的表)?这应该保留给管理员用户。您需要定期更新当地议会或其他机构的参考表。
Not really. You are mixing up two different functional aspects.
Normalise your data so that there is no duplication. otherwise your database is crippled.
Security. Why on Earth would you allow any person to update Reference (the tables that constitute static data that make up an Address) tables ? That should be reserved for Admin users. And periodically, you need to update the Reference tables from your local council or whatever.
恕我直言,你可以保留设计。
如果您的城市不正确,您的用户将会更新它。如果您认为用户也可能是错误的,只需等待 X 个人使用第一个用户提供的相同信息更新您的数据,然后相应地更新您的数据库。
IMHO you can keep the design.
If your city is incorrect, your users will update it. If you think the users could be wrong too, just wait for X persons to update your data with the same information the first user gave, then update your database accordingly.