1

我目前正在学习交易,我收到此错误消息:

BadRequestError:不支持嵌套事务

我不知道如何解决这个问题。任何帮助将不胜感激。

主文件

import webapp2
import jinja2
import os
import datetime

from google.appengine.ext import db

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):          
        transaction_status = create_message_txn("Python_Board", "message name", "Message Title", "Here is the message text")

        message = Message.all()

        template_values = {
            'transaction_status': transaction_status,
            'message': message,
        }       

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))


class MessageStore(webapp2.RequestHandler):
    def post(self):
        message = Message()
        message.message_title = self.request.get('message_title')       
        message.message_text = self.request.get('message_text')     
        photo.put() 
        self.redirect('/')      


class MessageBoard(db.Expando):
    pass


class Message(db.Expando):
    pass

def create_message_txn(board_name, message_name, message_title, message_text):
    board = db.get(db.Key.from_path('MessageBoard', board_name))
    if not board:
        board = MessageBoard(key_name=board_name)
        board.count = 0

    message = Message(key_name=message_name, parent=board)
    message.title = message_title
    message.text = message_text
    message.post_date = datetime.datetime.now()

    board.count += 1

    db.put([board, message])

    try:
        db.run_in_transaction(create_message_txn, board_name=board_name,
        message_name=message_title,
        message_title=message_title,
        message_text=message_text)
    except db.TransactionFailedError, e:
        transaction_status = "Transaction failed: %s " %e 

app = webapp2.WSGIApplication([('/', MainPage),
                                ('/new_message',MessageStore)], debug=True)

索引.html

<html>
    <body>                      
        <form action="/new_photo" method="post">            
            <label for="message_title">Message Title</label>
            <div><textarea name="message_title" rows="1" cols="60" id="message_title"></textarea></div>         
            <label for="message_text">Message Text</label>
            <div><textarea name="message_text" rows="3" cols="60" id="message_text"></textarea></div>                       
            <input type="submit" value="Submit">
        </form>
        <div><hr></hr></div>        
        <div><b>{{ transaction_status }}</b></div>          
        <div><hr></hr></div>            
        {% for m in message(limit=5): %}
        <div><b>{{ m.title }}</b></div>
        <div>{{ m.text }}</div>
        <div>{{ m.date }}</div>
        <div><hr></hr></div>        
        <b>{% endfor %}</b>
    </body>
</html>
4

1 回答 1

1

看起来你正在递归调用create_message_txn

def create_message_txn():

    # code happening here
    db.run_in_transaction(create_message_txn, board_name=board_name

^run_in_transaction调用create_message_txn哪个调用run_in_transaction哪个调用等...

您可以通过将要作为事务运行的代码分解为单独的函数来解决此问题

于 2013-03-24T19:48:05.793 回答