一种选择是使用Geopy查找某人(如Yahoo或Google Maps)的地址,然后该地址将返回与其匹配的地址的完整地址。您可能需要注意退回地址中的公寓号码被截断(例如“221 Amsterdam Av#330”变成“221 AMSTERDAM AVENUE”)。此外,您还将获得用户可能缩写或拼写错误的城市/州/国家/地区信息。
如果存在多个匹配项,您可以提示用户询问他们的地址是哪个。在没有匹配的情况下,您可以让用户知道,并且可能允许保存地址,具体取决于有效地址的重要性以及您对地址查找提供程序的有效性的信任程度。
至于这样做归一化的形式与模式,我不知道是什么做的事情的首选Django的方法是,但我的偏好是在形式,例如:
def clean(self):
# check address via some self-defined helper function
matches = my_helper_address_matcher(address, city, state, zip)
if not matches:
raise forms.ValidationError("Your address couldn't be found...")
elif len(matches) > 1:
# add javascript into error so the user can select
# the address that matches? maybe there is a cleaner way to do this
raise forms.ValidationError('Did you mean...')
你可能的情况下,扔在这个模型中查找函数(或一些helpers.py文件)你想重新使用它在其他领域
对于哪个国家/地区? – payne 2011-01-29 18:44:29
对不起:美国。特别是纽约市。 – 2011-01-30 00:04:09