web2py:多个表:有条件插入/更新/删除:从一种表单

发布于 2024-11-11 17:17:08 字数 4635 浏览 1 评论 0原文

我已经编写了用于管理条件插入/更新/删除的代码 “web2py”中单个表单的多个表。 我同意,代码是非常原始的形式&可能不是“pythonic”。 有代码重复。 但至少我有一些事情可以继续进行&打造精致的 结构。


型号:


db.define_table('mdlmst', 
              Field('mdlmstid','id'), 
              Field('mdlmstcd'), 
              Field('mdlmstnm'), 
              migrate=False, 
              format='%(mdlmstnm)s' 
              ) 

db.define_table('wrmst', 字段('wrmstid','id'), 字段('wrmstcd'), 字段('wrmstnm'), 迁移=假, 格式='%(wrmstnm)s' )

db.define_table('extwrmst', 字段('extwrmstid','id'), 字段('extwrmstcd'), 字段('extwrmstnm'), 迁移=假, 格式='%(extwrmstnm)s' )

从 FORM 中,数据将填充到以下两个表中


db.define_table('mdlwr', 
              Field('mdlwrid','id'), 
              Field('mdlmstid',db.mdlmst), 
              Field('wrmstid',db.wrmst), 
              migrate=False 
              ) 

db.define_table('mdlextwr', 字段('mdlextwrid','id'), 字段('mdlmstid',db.mdlmst), 字段('extwrmstid',db.extwrmst), 迁移=假 )

控制器:

呈现“mdlmst”表中的记录


def modelwar(): 
    models = db(db.mdlmst.mdlmstid>0).select(orderby=db.mdlmst.mdlmstnm) 
    return dict(models=models) 

“modelwar”控制器将在单击特定记录后

,“war_edit”控制器将管理表 –“mdlwr”和“mdlwr”。 'mdlextwr'


def war_edit(): 
    mdl_id = request.args(0)

mdl_id 是标识“mdlmstid”(要修改的记录)的变量

 
    mdl_nm = request.args(1)

mdl_nm 是用于获取“mdlmstnm”的变量

 
    warset = db(db.mdlwr.mdlmstid==mdl_id)        # fetch a set 
    extwarset = db(db.mdlextwr.mdlmstid==mdl_id)  # fetch a set 
    warlist = db(db.mdlwr.mdlmstid==mdl_id).select() # get a ROW object 
    extwarlist = db(db.mdlextwr.mdlmstid==mdl_id).select() # get a ROW object

form_war=FORM(TABLE(TR("Basic Warranty", 

SELECT(_type="select",_name="baswar",*[选项(x.wrmstnm,_value=x.wrmstid) for x in db().select(db.wrmst.ALL)]), TR("延长保修期", SELECT(_type="select",_name="extwar",*[选项(x.extwrmstnm,_value=x.extwrmstid) 对于 db().select(db.extwrmst.ALL)]) 中的 x, TR("", INPUT(_type='提交',_value='保存')), ))))

预先填充“form_war”中的字段


    if len(warlist)>0: 
        form_war.vars.baswar = warlist[0].wrmstid 
    if len(extwarlist)>0: 
        form_war.vars.extwar = extwarlist[0].extwrmstid 

成功提交表单后,管理表“mdlwr”


    if form_war.accepts(request.vars, session): 

从数据库中获取的列表中是否有任何记录?发送到表格,


        if len(warlist)>0:

如果从 FORM 字段返回的值为空,则删除,否则更新,


            if form_war.vars.baswar==''
                warset.delete() 
            else: 
                warset.update(wrmstid=form_war.vars.baswar)

否则插入

 
        else: 
            db.mdlwr.insert(mdlmstid=mdl_id, wrmstid=form_war.vars.baswar)

类似地,管理表“mdlextwr”


        if len(extwarlist)>0: 
            if form_war.vars.extwar=='': 
                extwarset.delete() 
            else: 
                extwarset.update(extwrmstid=form_war.vars.extwar) 
        else: 
            db.mdlextwr.insert(mdlmstid=mdl_id, extwrmstid=form_war.vars.extwar) 

    response.flash = 'Warranty definition saved' 
return dict(form_war=form_war,mdlnm=mdl_nm) 

查看“mdlmst”表


{{response.files.append(URL(r=request,c='static',f='jquery.dataTables.min.j­­ 
s'))}} 
{{response.files.append(URL(r=request,c='static',f='demo_table.css'))}} 
{{extend 'layout.html'}} 

jQuery(document).ready(function() { jQuery('.smarttable').dataTable();});

Modelwise Warranty Master

Model IDModel CodeModel Name {{for model in models:}} {{=model.mdlmstid}} {{=model.mdlmstcd}} {{=model.mdlmstnm}} {{=A('edit warranty',_href=URL('war_edit',args=[model.mdlmstid,model.mdlmstnm]))}} {{pass}}

Pl. tell me if I have coded anything stupid here. I would highly welcome any ideas/suggestions for improvements.

谢谢, 维内特

I have written code for managing conditional insert/update/delete to
multiple tables from single form in 'web2py'.
I agree, the code is in very raw form & may not be ‘pythonic’.
There are code repeatitions.
But at least I have something to go ahead & build a refined
structure.


MODELS:


db.define_table('mdlmst', 
              Field('mdlmstid','id'), 
              Field('mdlmstcd'), 
              Field('mdlmstnm'), 
              migrate=False, 
              format='%(mdlmstnm)s' 
              ) 

db.define_table('wrmst', Field('wrmstid','id'), Field('wrmstcd'), Field('wrmstnm'), migrate=False, format='%(wrmstnm)s' )

db.define_table('extwrmst', Field('extwrmstid','id'), Field('extwrmstcd'), Field('extwrmstnm'), migrate=False, format='%(extwrmstnm)s' )

from the FORM, data will be populated in the following two tables


db.define_table('mdlwr', 
              Field('mdlwrid','id'), 
              Field('mdlmstid',db.mdlmst), 
              Field('wrmstid',db.wrmst), 
              migrate=False 
              ) 

db.define_table('mdlextwr', Field('mdlextwrid','id'), Field('mdlmstid',db.mdlmst), Field('extwrmstid',db.extwrmst), migrate=False )

CONTROLLERS:

‘modelwar’ controller will render the records from ‘mdlmst’ table


def modelwar(): 
    models = db(db.mdlmst.mdlmstid>0).select(orderby=db.mdlmst.mdlmstnm) 
    return dict(models=models) 

after clicking a particular record, ‘war_edit’ controller will

manage the tables – ‘mdlwr’ & ‘mdlextwr’


def war_edit(): 
    mdl_id = request.args(0)

mdl_id is a variable identifying the ‘mdlmstid’ (which record to be modified)

 
    mdl_nm = request.args(1)

mdl_nm is a variable for getting the ‘mdlmstnm’

 
    warset = db(db.mdlwr.mdlmstid==mdl_id)        # fetch a set 
    extwarset = db(db.mdlextwr.mdlmstid==mdl_id)  # fetch a set 
    warlist = db(db.mdlwr.mdlmstid==mdl_id).select() # get a ROW object 
    extwarlist = db(db.mdlextwr.mdlmstid==mdl_id).select() # get a ROW object

form_war=FORM(TABLE(TR("Basic Warranty", 

SELECT(_type="select",_name="baswar",*[OPTION(x.wrmstnm,_value=x.wrmstid)
fo­r x in db().select(db.wrmst.ALL)]),
TR("Extended Warranty",
SELECT(_type="select",_name="extwar",*[OPTION(x.extwrmstnm,_value=x.extwrms­­tid)
for x in db().select(db.extwrmst.ALL)]),
TR("", INPUT(_type='submit',_value='Save')), ))))

pre-populate the fields in‘form_war’


    if len(warlist)>0: 
        form_war.vars.baswar = warlist[0].wrmstid 
    if len(extwarlist)>0: 
        form_war.vars.extwar = extwarlist[0].extwrmstid 

after successful form submission, manage the table 'mdlwr'


    if form_war.accepts(request.vars, session): 

if there was any record in the list fetched from database & sent to FORM,


        if len(warlist)>0:

delete if value returned from FORM field is blank, else update


            if form_war.vars.baswar==''
                warset.delete() 
            else: 
                warset.update(wrmstid=form_war.vars.baswar)

else insert

 
        else: 
            db.mdlwr.insert(mdlmstid=mdl_id, wrmstid=form_war.vars.baswar)

Similarly, manage the table 'mdlextwr'


        if len(extwarlist)>0: 
            if form_war.vars.extwar=='': 
                extwarset.delete() 
            else: 
                extwarset.update(extwrmstid=form_war.vars.extwar) 
        else: 
            db.mdlextwr.insert(mdlmstid=mdl_id, extwrmstid=form_war.vars.extwar) 

    response.flash = 'Warranty definition saved' 
return dict(form_war=form_war,mdlnm=mdl_nm) 

VIEW for 'mdlmst' table


{{response.files.append(URL(r=request,c='static',f='jquery.dataTables.min.j­­ 
s'))}} 
{{response.files.append(URL(r=request,c='static',f='demo_table.css'))}} 
{{extend 'layout.html'}} 

jQuery(document).ready(function() { jQuery('.smarttable').dataTable();});

Modelwise Warranty Master

Model IDModel CodeModel Name {{for model in models:}} {{=model.mdlmstid}} {{=model.mdlmstcd}} {{=model.mdlmstnm}} {{=A('edit warranty',_href=URL('war_edit',args=[model.mdlmstid,model.mdlmstnm]))}} {{pass}}


Pl. tell me if I have coded anything stupid here.
I would highly welcome any ideas/suggestions for improvements.

Thanks,
Vineet

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2024-11-18 17:17:08

你的数据库设计对我来说看起来很奇怪。

在每个表中都有一个“id”类型的字段。这将取代 web2py 自动生成的 id 字段 - 一个坏主意。来自web2py书:“不要声明一个名为“id”的字段,因为无论如何它都是由web2py创建的。每个表默认都有一个名为“id”的字段。它是一个自动递增整数字段(从1) 用于交叉引用并使每条记录都唯一,因此“id”是主键”

您已在表“mdlmst”和“wrmst”之间创建了多对多关系以及另一个多对多关系在“mdlmst”和“extwrmst”之间。虽然这不一定是错误的,但我觉得这极不可能是你想要的。

我的感觉是你的数据库设计需要改进。在开始设计表单之前应该解决这个问题。

Your database design looks strange to me.

In each table you have a field of type 'id'. This will replace the id field automatically generated by web2py - a bad idea. From the web2py book: "Do not declare a field called "id", because one is created by web2py anyway. Every table has a field called "id" by default. It is an auto-increment integer field (starting at 1) used for cross-reference and for making every record unique, so "id" is a primary key"

You have created a many to many relationship between table 'mdlmst' and 'wrmst' and another many to many relationship between 'mdlmst' and 'extwrmst'. While this is not necessarily wrong, it strikes me as extremely unlikely this is what you want.

My feeling is that your database design needs work. This should be sorted out before you start designing forms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文