开发者

Add or Update object from a form GAE python

开发者 https://www.devze.com 2023-04-06 11:11 出处:网络
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

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:<开发者_JAVA技巧/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


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:

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


You can use get_or_insert Model method. Also see this question.

0

精彩评论

暂无评论...
验证码 换一张
取 消