从表单GAE python添加或更新对象

时间:2011-09-22 07:23:38

标签: google-app-engine google-cloud-datastore

我尝试创建一个将表单中的数据添加到db.model的类,但我的问题是,当我尝试添加现有的书时,我想更新副本而不是添加新对象。我真的试图找到答案,我在GAE中阅读了编程书籍,但我找不到任何问题。 提前感谢您,如果有人使用示例代码回答而不是仅使用命令(例如,请尝试使用get_by_id()),我将非常感激。

以下是我来源的一部分

class Book(db.Model):
    book_id = db.IntegerProperty()
    title = db.StringProperty()
    author = db.StringProperty()
    copies = db.IntegerProperty()
    isbn = db.IntegerProperty()
    copyright_date = db.StringProperty()
    category = db.StringProperty()
    comments = db.StringProperty()



class Add(webapp.RequestHandler):        #Form to input data
    def get(self):
            self.response.out.write("""            
          <html>
            <body>
              <form action="/sign" method="post">
        <table border="0">
        <tr>
            <td>ISBN:</td> <td><input type="text" name="isbn"</td>
        </tr>
                <tr>
            <td>Title:</td> <td><input type="text" name="title"</td>
        </tr>
        <tr>
            <td>Author:</td> <td><input type="text" name="author"</td>
        </tr>
        <tr>
            <td>Copies:</td> <td><input type="text" name="copies"</td>
        </tr>
        <tr>
            <td>Copyright Date:</td> <td><input type="text" name="copyright_date"</td>
        </tr>
        <tr>
            <td><div>Category:</td> <td><select>
            <option name="category" value="adventure">Adventure</option>
            <option name="category" value="comedy">Comedy</option>
            <option name="category" value="dramatic">Dramatic</option>
            <option name="category" value="mystery">Mystery</option>
            <option name="category" value="science_fiction">Science Fiction</option></select></td>
        </tr>        
            <tr><td>Comments:</td></tr></table>
            <div><textarea name="comments" rows="5" cols="40" value="Add your comments here"></textarea></div>
            <div><input type="submit" value="Add Book">
            <input type="reset" value="Reset"></div>

              </form>
            </body>
          </html>""")




class Guestbook(webapp.RequestHandler):
  def post(self):
    book = Book()
    book.isbn = int(self.request.get('isbn')) 
    book.title = self.request.get('title')
    book.author = self.request.get('author')
    book.category = self.request.get('category')
    book.copies = int(self.request.get('copies'))
    book.copyright_date = self.request.get('copyright_date') 
    book.put()      



class Stock(webapp.RequestHandler):
    def get(self):                    #Retrieve all data from DataBase (Here is the StockHouse)
        Book = db.GqlQuery("SELECT * FROM Book ")
        for book in Book:
            print book.book_id, book.isbn, book.title, book.author, book.category, book.copyright_date, book.copies, book.comments

2 个答案:

答案 0 :(得分:0)

为了更新现有条目,您需要一个不同的处理程序,它将获取post参数以及要更新的实体的id或key。通常这是在url中,例如:

POST / updateentity / id

然后在处理程序中:

def get(self, id):
  existing = Model.get_by_id(long(id))
  model.field = self.request.get("field")
  model.put()

答案 1 :(得分:0)

您可以使用get_or_insert Model方法。另请参阅this question