I have a primitive data model with users that can make inserts that should have a city and a region. To prepare for articles that are connected with several cities (so that for instance a product offering that is the same in two or more cities will become only one article with a list of cities instead of duplicated articles for every single city that the article is connected to).
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()
So I could manage to make the storage and the views but the data model is not good.
class Article(db.Model):
cities = db.ListProperty(db.Key)
regions = db.ListProperty(db.Key)
At the insert it is coded:
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()
This generates redundancy and is not very pretty (and not in 1NF since it it saved a list in a field).
When building the index for the search API I so far only use one city, but I plan to handle lists of cities and lists of regions (although a city can never be in two regions, so everything but a city list is actually redundand, but I save erdundancy to avoid lengthy lookups at searches and views). I wonder if I used referenceproperties and keys correctly and if I would be better off using the NDB models instead?