静岡市のゴミ回収日は可燃ゴミや不燃ゴミ、缶ビンなどのゴミの種類によって区分が異なります。例えば、駿河区の石田2〜3丁目は不燃物の収集日は同じですが、可燃物、ビン缶の回収日は石田街道以東と石田街道以西で変わります。可燃ゴミ、不燃ゴミ、缶瓶ゴミの回収日を個別のモデルで定義しリレーションするためにApp EngineのReferencePropertyでどのように実装できるかメモしておきます。
class Sz_gomi(db.Model):
initial = db.StringProperty()
jichitai = db.StringProperty()
kanen = db.StringProperty()
binkan = db.StringProperty()
kstart = db.StringProperty()
bstart = db.StringProperty()
chomei = db.StringProperty()
class Sz_funen(db.Model):
funen = db.StringProperty()
fstart = db.StringProperty()
szgomi = db.ReferenceProperty(Sz_gomi, collection_name="funens")
class SzEntry(webapp2.RequestHandler):
def post(self):
rawfile = self.request.get('file')
csvfile = csv.reader(StringIO(rawfile),dialect='excel')
for row in csvfile:
s = Sz_gomi(key_name = str(row[1]),
initial=unicode(row[0],'UTF-8'),
jichitai=unicode(row[2],'UTF-8'),
kanen=unicode(row[3],'UTF-8'),
binkan=unicode(row[4],'UTF-8'),
chomei=unicode(row[5],'UTF-8'),
kstart=unicode(row[6],'UTF-8'),
bstart =unicode(row[7],'UTF-8')
)
s.put()
self.redirect(self.request.uri)
class SzFunenEntry(webapp2.RequestHandler):
def post(self):
rawfile = self.request.get('file')
csvfile = csv.reader(StringIO(rawfile),dialect='excel')
for row in csvfile:
#ReferencePropertyにはquery-objectを渡す
chiku = db.Query(Sz_gomi).filter('chomei = ',unicode(row[0],'utf-8')).get()
Sz_funen(chomei=unicode(szgomi = chiku,
funen=unicode(row[1],'utf-8'),
fstart=unicode(row[2],'utf-8')
).put()
class sample(webapp2.RequestHandler):
def get(self):
szs = Sz_gomi.all()
tmp_value={'szs':szs}
template = JINJA_ENVIROMENT.get_or_select_template('sample.html')
self.response.write(template.render(tmp_value))
<!--とりあえず自治会名ー地区名のリストを書き出してみます。-->
<ul>
{% for sz in szs %}
<li>{{sz.jichitai}}:
<!--collenction_nameでReferenceProperty型のオブジェクトを取得-->
{% for x in sz.funens %}
{{x.funen}}
{% endfor %}
</li>
{% endfor %}
</ul>