从表单 GAE python 添加或更新对象
我尝试创建一个类,它将数据从表单添加到 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
I try to create a class which will add data from a form to a db.model, but my problem is that when I will try to add an existing book, I want to update the copies and not to add a new object. I really try to find an answer, I read books for programming in GAE, but I find nothing for my problem.
Thank you in advance and I will really appreciate if someone answer with a sample code and not with just a command ( eg try get_by_id()).
Here is a part of my source
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了更新现有条目,您需要一个不同的处理程序,该处理程序将采用 post 参数以及要更新的实体的 id 或键。通常这是在 url 中,例如:
POST /updateentity/id
然后在处理程序中:
in order to update an existing entry you'll need a different handler that will take the post arguments as well as the id or key of the entity to update. Usually this is in the url, e.g:
POST /updateentity/id
and then in the handler:
您可以使用 get_or_insert
模型方法。另请参阅此问题。
You can use get_or_insert
Model
method. Also see this question.