Component error SQLFORM with record

155 views
Skip to first unread message

LaDarrius Stewart

unread,
May 16, 2014, 1:27:47 AM5/16/14
to web...@googlegroups.com
Im loading a component from a grid with a button you can see the code below in grid "links=links". My issue is the first time I press the button everything is fine the pages collapse etc etc the component loads. Then if i press another button from the grid im getting a strange error(Seen below). The crazy thing about the error is whats causing the issue is when I pass the SQLFORM a record value(See below). It gives me the following error it seems as if its concatenating the ids. If I take the record value off the form I no longer receive the error. I've been stuck on this for hours I cant see why making this a edit FORM destroys the code. If I take the record value you out im able to click the buttons fine a million times no issue. As soon as the record value is in place it goes haywire =[

Error Message:
 "<type 'exceptions.ValueError'> invalid literal for long() with base 10: '|19193|860|860|'"

form:
a = request.args(0)
form=SQLFORM(db.Company,a, formstyle="bootstrap")

grid link:
links = [lambda row: BUTTON('Profile',_onclick="jQuery('#id5').val('%s'); ajax('new', ['id'], 'ab');" % str(row.id), **{'_data-toggle': 'collapse', '_data-target' : '#ab', '_class' : 'two btn btn-success', '_type' : 'submit', '_value' : 'Submit', '_id' : 'ld'} )]

Niphlod

unread,
May 16, 2014, 3:04:33 PM5/16/14
to web...@googlegroups.com
are you aware that ajax('new' ['id'], 'ab') it takes all input with name = id and pass those as an argument ?

LaDarrius Stewart

unread,
May 16, 2014, 3:23:12 PM5/16/14
to web...@googlegroups.com
Yeah there is only that one. Also there our other lines using variable "a" and they are acting fine each time I click another button. Like "Company = db(db.Company.id==a).select().first()" so its not that the value is incorrect because everything else it works fine with. Its only with the SQLFORM update form that I receive an issue when I pass it the record value. Without that record value in SQLFORM there our  multiple queries that take place with the "a" variable and they're fine. Could this be a bug or am I overlooking something?

Anthony

unread,
May 16, 2014, 3:59:50 PM5/16/14
to web...@googlegroups.com
It might help if you can show more of the controller code and the view code so we can get a better idea of what's going on.

LaDarrius Stewart

unread,
May 16, 2014, 4:27:10 PM5/16/14
to web...@googlegroups.com
links = [lambda row: BUTTON('Profile',_onclick="jQuery('#id5').val('%s'); ajax('new', ['id'], 'ab');" % str(row.id), **{'_data-toggle': 'collapse', '_data-target' : '#ab', '_class' : 'two btn btn-success', '_type' : 'submit', '_value' : 'Submit', '_id' : 'ld'} )] # Companysearch method grid links to create button

def new():
    return LOAD('default','companypage.load',ajax=False, ajax_trap=True, args=[request.vars.id]) 

def companypage():
    a = request.args(0)
    company = db((db.Company.id==a)).select().first()
    record = db.Company(request.args(0)) 
    form3=SQLFORM(db.Company,record=record,formstyle="bootstrap")
    if form3.process(session=None, formname='form123213').accepted:
        response.flash = 'Working'
    return locals()

<form><input class="" id="id5" name="id" value="" /></form> #companysearch.html
<div class="collapse" id="ab"> </div> #also within companysearch.html


LaDarrius Stewart

unread,
May 16, 2014, 4:28:49 PM5/16/14
to web...@googlegroups.com
Omitted quite a bit Anthony but I feel I included the valuable information. Considering minus the record value of the SQLFORM the functionality works as intended.
Message has been deleted

LaDarrius Stewart

unread,
May 16, 2014, 5:27:20 PM5/16/14
to web...@googlegroups.com
I deleted previous post stating it was working it's not im tired...

Massimo Di Pierro

unread,
May 16, 2014, 11:09:04 PM5/16/14
to web...@googlegroups.com
I do not think the issue with your code. The issue with corrupted data in database. You seem to have an integer that contains list data. Perhaps one of your fields was a "list:integer" or "list:reference" type and then you changed it to "integer" or "reference" type. You ended up with corrupted data in there. A "normal" database like postgresql would have warned you of something like this and prevent such migration. I assume you are using sqlite which does allow inconsistent data types. Try delete (or move) your database and see if you can reproduce the problem.

LaDarrius Stewart

unread,
May 17, 2014, 9:37:15 AM5/17/14
to web...@googlegroups.com
Thanks Massimo for shining light on the situation. After your reply i went ahead and tried b = int(a) and received an error. TypeError: int() argument must be a string or a number, not \'list\'\n'
So after that I started fresh with a new database since the previous database had imported data. I only inserted two records via appadmin ran through everything again and im still getting the same issue with the ids turning into a list. The database is MySQL hosted via PA and im running web2py 2.8.2. Table definition is below as well. When I just view a=request.args(0) by itself its not concatenating the ids into a list its displaying the id as expected onclick. I test it with sqlite as well and same issue.
db.define_table('Company',
                Field('Name', 'text', requires=IS_NOT_EMPTY()),
                Field('Address', 'text'),
                Field('Address2', 'text',),
                Field('City', 'text'),
                Field('State', 'text'),
                Field('Zip', 'integer'),
                Field('Email', 'text'),
                Field('Website', 'text'),
                Field('CouncilMember', 'boolean', default=False),
                Field('Contact', 'text'),
                Field('Phone', 'text'),
                Field('Fax', 'text'),
                auth.signature)

LaDarrius Stewart

unread,
May 17, 2014, 10:07:31 AM5/17/14
to
FYI Inside the new method i added i tried a=int(request.vars.id) to see if I could produce the same list error and it does as well.
aundefined
global request<Storage {'_vars': <Storage {'id': ['2', '1']}>,...'}>
builtinint<type 'int'>
request.vars.id['2', '1']
request.vars<Storage {'id': ['2', '1']}>

Fixed the issue with the following:
    if type(request.vars.id) is not list:
        a=int(request.vars.id)
    else:
        a=int(request.vars.id[0])

Still unsure why thats occurring though but thats how i fixed it.

Anthony

unread,
May 17, 2014, 10:27:35 AM5/17/14
to web...@googlegroups.com
It's still not quite clear what's going on, but this approach seems unnecessarily indirect (i.e., copying the row ID to an input field, and then using the ajax function to send that value to a function that returns a component, which then calls another function to produce a form).

Instead, why not just create an Ajax component directly upon button click:

links = [lambda row: BUTTON('Profile',

                            _onclick
="jQuery.web2py.component('%s', 'ab');" % \
                                     URL
('default', 'companypage.load', args=row.id),

                           
**{'_data-toggle': 'collapse', '_data-target' : '#ab',
                               
'_class' : 'two btn btn-success', '_type' : 'submit',
                               
'_value' : 'Submit', '_id' : 'ld'})]

In that case, you no longer need the new() function, and you can also get rid of the form with the "id5" input field. jQuery.web2py.component() will make an Ajax call to /default/companypage.load/[row.id], which will return the form for that record and load it in the "ab" div.

Anthony

LaDarrius Stewart

unread,
May 17, 2014, 11:10:08 AM5/17/14
to web...@googlegroups.com
Thank you Anthony this is much better greatly appreciated.

LaDarrius Stewart

unread,
May 17, 2014, 11:44:19 AM5/17/14
to web...@googlegroups.com
FYI took your concept and changed it around a bit for anyone else that might have been trying.
links = [lambda row: BUTTON('Profile',_onclick="ajax('%s', [], 'ab');" % URL('default', 'companypage.load', args=[row.id]), **{'_data-toggle': 'collapse', '_data-target' : '#ab', '_class' : 'accordion-toggle btn btn-success', '_data-parent' : '#accordian', '_type' : 'submit', '_value' : 'Submit', '_id' : 'ld'})]
Reply all
Reply to author
Forward
0 new messages