Python Web2py CRUD.read()通过非填充条件创建的记录

Python Web2py CRUD.read()通过非填充条件创建的记录,python,crud,web2py,Python,Crud,Web2py,因此,我在Web2py中编写了一个函数,在某个条件下将记录创建到数据库中的表中,但Web2py会创建记录,尽管该条件未被填充 function_1(....) ... ... update_field(db,auth,'settings','account_balance',-price) response.flash='Done' redirect(URL('products'))

因此,我在Web2py中编写了一个函数,在某个条件下将记录创建到数据库中的表中,但Web2py会创建记录,尽管该条件未被填充

        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass
下面是函数

def buy_product():
    price = price_set(db,auth,'product')
    balance = limit(db,auth,'settings','account_balance')
    if balance !=None:
        if balance < price:
            form=redirect(URL('order'))
        else:
            form=crud.create(db.letter)
            if form.accepts(request.vars, session):
                tax = float(postage(form.vars.tax_type).replace("-","."))
                ##########################
                # I'm talking about this #
                ##########################
                if balance < (price + tax):
                    response.flash='You don\'t have enough balance to buy this product'
                    redirect(URL('not_processed'))
                else:
                    function_1(....)
                    ...
                    ...
                    update_field(db,auth,'settings','account_balance',-price)
                    response.flash='Done'
                    redirect(URL('products'))
                    pass
            elif form.errors:
                response.flash='Error 01'
            else:
                pass
            ###############################
    else:
        form=redirect(URL('settings'))
    return dict(form=form)
        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass
有什么想法吗

        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass

谢谢

Crud在内部管理插入/更新,并且不使用表单。接受

        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass
您有两个选择:

        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass
1-使用SQLFORM

        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass
    form=SQLFORM(db.letter)
    if form.accepts(request.vars, session):
        tax = float(postage(form.vars.tax_type).replace("-","."))
        ##########################
        # I'm talking about this #
        ##########################
2-使用crud事件

        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass
def myfunction(form):
    # do your stuff here
    # it will be called only when form is accepted

def myotherfunction(form):
    if form.errors:
        #do something here in case of errors
        #it will be called during the form validation

crud.settings.create_onvalidation = myotherfunction
crud.settings.create_onaccept = myfunction
#the above can be:
#crud.create_onaccept = lambda form: myfunction(form)

# define the above events before the creation of the form
form=crud.create(db.letter)

请注意,SQLFORM与crud有点不同,SQLFORM希望您使用form.accepts方法验证表单,而crud在内部进行验证,并使用事件作为onvalidation、onaccept来接受自定义验证。

您还应该能够这样做:
form=crud.create(db.letter,onvalidation=myvalidationfunction)
SQLFORM和crud都接受onvalidation参数。我相信,如果使用
form.errors.field=“Cannot do”
无法满足这些条件,那么您可以检查额外的条件并添加错误。我之所以提到它,是因为您可能不希望所有CRUD都使用单一的onvalidation函数,并且您可能有充分的理由使用CRUD而不是SQLFORM(SQLFORM也接受onvalidation参数)。请参阅:了解更多信息
        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass