1

私は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)
4

1 に答える 1

0

振り返ってみると、解決策は非常に単純でした。エントリをデータストアに追加する場合、データストアはエンティティ key_id または key_name をキーとして使用できます。キー名を使用したい場合は、put コードに key_name = yourkeystring を追加するだけです。

  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('Content: %s <br>' % entry.content)
      self.response.out.write("""
            <form action="/sign" method="post">
                  <div class="field-field">
                  <select name="yourkeystring" id="yourkeystring">
                    <option value="ID1">ID1</option>
                    <option value="ID2">ID2</option>
                    <option value="ID3">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):
          n = Options(
            content = self.request.get('content'),
            key_name = self.request.get('yourkeystring'))
          n.put()
          self.redirect('/')


  app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/sign', Optionbook)
  ], debug=True)
于 2013-02-09T21:00:11.623 に答える