我有一个原始数据模型,用户可以插入应该有城市和地区的内容。为与多个城市相关联的文章做准备(例如,在两个或多个城市中提供相同的产品将成为只有一篇带有城市列表的文章,而不是针对该文章所连接的每个城市的重复文章至)。
class Region(db.Model):
name = db.StringProperty()
countrycode = db.StringProperty()
vieworder = db.IntegerProperty() # custom ORDER BY variable to order by population
areacode = db.IntegerProperty()
areacodes = db.ListProperty(int)
class City(db.Model):
region = db.ReferenceProperty()
name = db.StringProperty()
vieworder = db.IntegerProperty()
areacode = db.IntegerProperty()
所以我可以设法制作存储和视图,但数据模型不好。
class Article(db.Model):
cities = db.ListProperty(db.Key)
regions = db.ListProperty(db.Key)
在插入时,它被编码:
if self.request.get('area'):
city = model.City.get_by_id(long(self.request.get('area')))
region = model.Region.get(city.region.key())
article.cities.append(city.key())
article.regions.append(region.key())
article.city = unicode(city.name)
article.region = unicode(region.name)
article.put()
这会产生冗余并且不是很漂亮(而不是在 1NF 中,因为它在字段中保存了一个列表)。
在为搜索 API 构建索引时,到目前为止我只使用一个城市,但我计划处理城市列表和区域列表(虽然一个城市永远不可能在两个区域中,所以除了城市列表之外的所有内容实际上都是多余的,但是我节省了冗余以避免在搜索和视图中进行冗长的查找)。我想知道我是否正确使用了引用属性和键,以及使用 NDB 模型是否会更好?