使用带有可选签名的 SQLFROM.grid 在另一个表中插入多个数据 [英] Insert multiple data using SQLFROM.grid with selectable signature in another table

查看:15
本文介绍了使用带有可选签名的 SQLFROM.grid 在另一个表中插入多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

祝所有stackoverflow web2py Guru在这里的好日子....

Good day to all stackoverflow web2py Guru's here....

我发现了这个链接及其 2现在岁了.我有一个问题,我不知道如何编码.

I found this link and its 2 years old now. I have a problem and I don't know how to code it.

我有 2 个实体(表),这是一个 M:M 关系

I have 2 Entities(Tables) and it's a M:M relationship

第一张表:讲师(顾问)第二张表:学生名单

First table: The Instructors (Advisers) Second table: The Lists of Students

现在Advisers处理了很多学生,学生也有很多Advisor,对吗?

now Advisers handled many students and students have many advisers right?

所以我创建了第三个表,并将其命名为 Student_Adviser

so I create a third table and I named it to Student_Adviser

学年

db.define_table('school_year',
            Field('sy',),
            Field('current_year', 'boolean'))

学生名单

db.define_table('student_list',
            Field('lastname'),
            Field('firstname'))

这些是 Student_Adviser 中的字段

these are the fields in Student_Adviser

db.define_table('stud_adviser',
            Field('sy_id', 'reference school_year', label='School Year'),
            Field('adv_id', 'reference auth_user', label='Adviser'),
            Field('stud_id', 'reference student_list', label='Student', unique=True)
           )

在控制器中

def getStudent():
    form = SQLFORM.grid(db.Student_List, csv=False, create=False, selectable=(need code here))
    return locals()

我问这种问题是因为它可以帮助顾问通过激活多个复选框来获取学生列表,因此在他/她选择学生后,他/她将单击提交按钮,viola 所有选中的数据将自动添加到 Student_Adviser 表.我的想法在 web2py 中可行吗?

I ask this kind of question because it help the Advisers to get the list of students by activating multiple check boxes so after he/she choose the students he/she will click the submit button and viola all the checked data will automatically add to the Student_Adviser table. Is my idea is possible in web2py?

附加 1:

还请添加一个代码,该代码将自动插入已登录的 auth_user.id.

Please also add a code that will automatic insert also the auth_user.id who is logged-in.

假设当 Instructor 1 auth_user.id 为 1

Let's say when Instructor 1 auth_user.id is 1

教师 1 已登录,因此他/她将执行的所有事务都将 Student_Adviser 表中 adv_id 的默认值始终为 1,依此类推.

Instructor 1 is logged-in so all the transaction he/she will do the default value of adv_id in Student_Adviser table will always be 1 and so on.

附加 2:

当我尝试使用 db 接口在 Student_Adviser 表中手动添加数据时出现错误.

I'm getting an error when I tried to manual add the data in the Student_Adviser table using the db interface.

顺便说一句,我将如何发布回溯错误?代码示例在 Traceback 中不起作用.我无法发布错误,因为它会破坏格式...但这是错误代码的最后一行(请基于 Student_Adviser 中的表格).

btw how will I post a Traceback error? the Code Sample won't work in Traceback. I can't post the error because it will destroy the format... but this is the last line of error code (please based the table in Student_Adviser).

IntegrityError: foreign key constraint failed

推荐答案

应该这样做:

@auth.requires_login()
def getStudent():
    db.stud_adviser.sy_id.default = db.school_year(current_year=True).id
    db.stud_adviser.adv_id.default = auth.user_id
    def add_students(ids):
        for id in ids:
            db.stud_adviser.insert(stud_id=id)
    form = SQLFORM.grid(db.student_list, create=False, selectable=add_students,
                        csv=False)
    return dict(form=form)

selectable 参数是一个回调函数,它接收网格中选择的记录 ID 列表.作为该参数提供的 add_students 函数循环遍历 ID,并在 stud_adviser 表中为每个 ID 插入一个新记录.因为每条记录的学年和导师 ID 应该是相同的,所以它们是通过设置各自字段的 default 属性来设置的(对于学年,我假设您想要当前学校的 IDyear) -- 通过从 .insert() 调用中排除这些字段,将自动插入默认值.

The selectable argument is a callback function, which receives the list of record IDs selected in the grid. The add_students function supplied as that argument loops through the IDs and inserts a new record in the stud_adviser table for each one. Because the school year and adviser IDs should be the same for each record, they are set by setting the default attributes of their respective fields (for the school year, I assume you want the ID of the current school year) -- by excluding those fields from the .insert() call, the default values will be inserted automatically.

注意,要在网格中显示更多有用的详细信息(而不是学年、顾问和学生记录 ID),您可以在每个表上定义格式"属性:

Note, to display more useful details in the grid (rather than the school year, adviser, and student record IDs), you can define the "format" attribute on each of the tables:

db.define_table('school_year',
     Field('sy'),
     Field('current_year', 'boolean'),
     format='%(sy)s')

db.define_table('student_list',
     Field('lastname'),
     Field('firstname'),
     format='%(lastname)s')

因为 db.auth_user 表是自动定义的(具有默认的格式"属性),您必须事后更改其格式"属性——因此,在调用 之后的某处auth.define_tables():

Because the db.auth_user table is defined automatically (with a default "format" attribute), you must change its "format" attribute after the fact -- so, somewhere after calling auth.define_tables():

db.auth_user._format = '%(last_name)s'

使用如上定义的格式"属性,现在任何引用这些表的引用字段都将获得基于引用表的格式"属性的默认表示"属性.这将控制网格(以及 SQLFORM 和 SQLTABLE)中引用字段值的显示.

With the "format" attributes defined as above, now any reference field that references these tables will get a default "represent" attribute based on the "format" attribute of the referenced table. This will control the display of the reference field values in the grid (as well as in SQLFORMs and SQLTABLEs).

这篇关于使用带有可选签名的 SQLFROM.grid 在另一个表中插入多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆