how to insert dynamic record id into field in form

53 views
Skip to first unread message

Alex Glaros

unread,
Mar 10, 2015, 3:49:05 PM3/10/15
to web...@googlegroups.com
I want to insert the record id into one of its fields during record creation in form

right now, using work-around right after the record is created.

here is the record being created

messageID = db.InternalMessage.insert(**db.InternalMessage._filter_fields(form.vars))

right afterwards, this is executed

db(db.InternalMessage.id == messageID.id).update(messageThreadID = messageID.id)

it finds the record just created and updates it.

so I want field messageThreadID field to be populated with the record id field. Can it be done using less resources?  Is there a way to put the id in form.vars so that the top line above is all that's needed?

thanks

Alex Glaros

Niphlod

unread,
Mar 10, 2015, 4:37:18 PM3/10/15
to web...@googlegroups.com
there's something wrong with the code

something = db.table.insert(**mapping)

returns the db.table.id that has been inserted. in this piece of code, using messageID.id doesn't really make much sense...

Alex Glaros

unread,
Mar 10, 2015, 5:05:15 PM3/10/15
to web...@googlegroups.com
I don't know enough to be able to reply.  Here is code that works.   Please let me know if there's better way.   messageID captures the record ID so it can populate children further down the process

 db.InternalMessage.created_by.default = auth.user_id
    db.InternalMessage.subjectLine.label=''
    db.InternalMessage.messageBody.label=''
##    db.InternalMessage.messageThreadID.default = db.InternalMessage.id    # didn't work
    form=SQLFORM.factory(db.InternalMessage, labels=None)   
    if form.process().accepted: 
        recipientCount = db(db.TempInternalMessageRecipient.thisSessionID == response.session_id).count()
        if recipientCount == 0:
            session.flash='You did not select message recipients' 
            redirect(URL('forgot_to_add_message_recipient'))      
        messageID = db.InternalMessage.insert(**db.InternalMessage._filter_fields(form.vars))
        form.vars.messageID=messageID
        selectedIndividuals = db((db.TempInternalMessageRecipient.thisSessionID == response.session_id) & (db.auth_user.id == db.TempInternalMessageRecipient.recipientOfMessage)).select()
        for row in selectedIndividuals:
            db.InternalMessageRecipient.insert(recipientOfMessage=row.auth_user.id, messageID=messageID)
        session.flash='Your message has been sent.'
        db(db.InternalMessage.id == messageID.id).update(messageThreadID = messageID.id)
        db(db.TempInternalMessageRecipient.thisSessionID == response.session_id).delete()        
        redirect(URL('information_center'))
    return locals()


Niphlod

unread,
Mar 10, 2015, 5:20:41 PM3/10/15
to web...@googlegroups.com
uhm. there's a hiccup in the process (or at least in what you think should be the process).
You're using factory(), so you need to handle your own db i/o.
If you want to insert a new record, how can you know in advance its id if you didn't actually insert it into the table ? There is no way around it.

Alex Glaros

unread,
Mar 10, 2015, 5:29:45 PM3/10/15
to web...@googlegroups.com
that's what I needed to know

thanks Niphlod

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/Eha7_vPh1E0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gray Kanarek

unread,
Mar 11, 2015, 2:07:02 PM3/11/15
to web...@googlegroups.com
You can probably use the compute keyword to the particular field. http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Computed-fields
Reply all
Reply to author
Forward
0 new messages