私はpythonとGAEを学んでいます。基本的なアプリを作成できましたが、偶発的に重複したエントリが発生しています。よくわかりませんが、キー名を使用してこれに対処できると思いますが、まだ学習中であり、キー名の説明で見つけた例に混乱しています。
私がやりたいのは、文字列を「エントリ ID」として使用して、エントリが投稿される前に、スクリプトが同じエントリ「idstring」を持つエントリがまだ存在しないことを確認することです。リストを作成し、リストに対してエントリをチェックする厄介なコードを作成しましたが、これを行うためのより効率的な方法があると思いますか?
これが私のコードです:
import webapp2
from google.appengine.ext import db
class Options(db.Model):
idstring = db.StringProperty()
content = db.StringProperty(multiline=True)
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
options = db.GqlQuery("SELECT * "
"FROM Options "
"LIMIT 10")
for entry in options:
self.response.out.write('%s ' % entry.idstring)
self.response.out.write('Content: %s <br>' % entry.content)
self.response.out.write("""
<form action="/sign" method="post">
<div class="field-field">
<select name="idstring" id="id_string">
<option value="unique1">ID1</option>
<option value="unique2">ID2</option>
<option value="unique3">ID3</option>
</select>
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Submit"></div>
</form>
</body>
</html>""")
class Optionbook(webapp2.RequestHandler):
def post(self):
idlist = Options.all()
idlist2 = []
for i in idlist:
idlist2.append(i.idstring)
idstring = self.request.get('idstring')
if idstring not in idlist2:
entry = Options()
entry.content = self.request.get('content')
entry.idstring = self.request.get('idstring')
entry.put()
self.redirect('/')
app = webapp2.WSGIApplication([
('/', MainPage),
('/sign', Optionbook)
], debug=True)