Web2py Form Error! PLEASE HELP!!!! => long() argument must be a string or a number, not 'Table'

346 views
Skip to first unread message

Gideon George

unread,
Aug 10, 2014, 8:42:49 PM8/10/14
to
I have to copy the error ticket and paste it here, please help me. It's been killing me for days! I can't write a post and post it to the database of the form crud.create(db.table)
All the tutorials I follow used it perfectly but I have never used it successfully. What is happening?

"

<type 'exceptions.TypeError'> long() argument must be a string or a number, not 'Table'

Version

web2py™Version 2.9.5-stable+timestamp.2014.03.16.02.35.39
PythonPython 2.7.5+: /usr/local/bin/uwsgi (prefix: /usr)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
Traceback (most recent call last):
File "/home/GideonG/web2py/Gidigba/gluon/restricted.py", line 220, in restricted
exec ccode in environment
File "/home/GideonG/web2py/Gidigba/applications/Gidigba/controllers/default.py", line 230, in <module>
File "/home/GideonG/web2py/Gidigba/gluon/globals.py", line 385, in <lambda>
self._caller = lambda f: f()
File "/home/GideonG/web2py/Gidigba/applications/Gidigba/controllers/default.py", line 194, in timeline
if form.accepts(request):
File "/home/GideonG/web2py/Gidigba/gluon/sqlhtml.py", line 1592, in accepts
self.vars.id = self.table.insert(**fields)
File "/home/GideonG/web2py/Gidigba/gluon/dal.py", line 9114, in insert
ret = self._db._adapter.insert(self, self._listify(fields))
File "/home/GideonG/web2py/Gidigba/gluon/dal.py", line 1353, in insert
query = self._insert(table,fields)
File "/home/GideonG/web2py/Gidigba/gluon/dal.py", line 1344, in _insert
values = ','.join(self.expand(v, f.type) for f, v in fields)
File "/home/GideonG/web2py/Gidigba/gluon/dal.py", line 1344, in <genexpr>
values = ','.join(self.expand(v, f.type) for f, v in fields)
File "/home/GideonG/web2py/Gidigba/gluon/dal.py", line 1552, in expand
return str(self.represent(expression,field_type))
File "/home/GideonG/web2py/Gidigba/gluon/dal.py", line 2014, in represent
return str(long(obj))
TypeError: long() argument must be a string or a number, not 'Table'

Error snapshot help

<type 'exceptions.TypeError'>(long() argument must be a string or a number, not 'Table')

inspect attributes

Frames

  • File /home/GideonG/web2py/Gidigba/gluon/restricted.py in restricted at line 220 code arguments variables

  • File /home/GideonG/web2py/Gidigba/applications/Gidigba/controllers/default.py in <module> at line 230 code arguments variables

  • File /home/GideonG/web2py/Gidigba/gluon/globals.py in <lambda> at line 385 code arguments variables

  • File /home/GideonG/web2py/Gidigba/applications/Gidigba/controllers/default.py in timeline at line 194 code arguments variables

  • File /home/GideonG/web2py/Gidigba/gluon/sqlhtml.py in accepts at line 1592 code arguments variables

  • File /home/GideonG/web2py/Gidigba/gluon/dal.py in insert at line 9114 code arguments variables

  • File /home/GideonG/web2py/Gidigba/gluon/dal.py in insert at line 1353 code arguments variables

  • File /home/GideonG/web2py/Gidigba/gluon/dal.py in _insert at line 1344 code arguments variables

  • File /home/GideonG/web2py/Gidigba/gluon/dal.py in <genexpr> at line 1344 code arguments variables

  • File /home/GideonG/web2py/Gidigba/gluon/dal.py in expand at line 1552 code arguments variables

  • File /home/GideonG/web2py/Gidigba/gluon/dal.py in represent at line 2014 code arguments variables

    Function argument list

    (self=<gluon.dal.MySQLAdapter object>, obj=<Table auth_user (id,first_name,last_name,email,..._government,city_of_resident,image,phone_number)>, fieldtype='reference auth_user')

    Code listing
    2009.
    2010.
    2011.
    2012.
    2013.
    2014.

    2015.
    2016.
    2017.
    2018.
                return str(obj)
    elif field_is_type('reference'): # reference
    # check for tablename first
    referenced = fieldtype[9:].strip()
    if referenced in self.db.tables:
    return str(long(obj))

    p = referenced.partition('.')
    if p[2] != '':
    try:
    ftype = self.db[p[0]][p[2]].type
"

Anthony

unread,
Aug 11, 2014, 4:12:33 AM8/11/14
to web...@googlegroups.com
Please show your code.

Leonel Câmara

unread,
Aug 11, 2014, 4:59:27 AM8/11/14
to
Without seeing any code I can only guess. My guess is that you have something like this before the SQLFORM process.

db.table_you_are_inserting.creator.default = db.auth_user

When it should be something like:
db.table_you_are_inserting.creator.default = auth.user_id

Gideon George

unread,
Aug 11, 2014, 12:54:02 PM8/11/14
to web...@googlegroups.com
This is my action in default.py (Everything is working fine, except the form in the view, if I write and submit the post, it gives this error above)
@auth.requires_login()
def timeline():
    db.post.posted_by.default =  auth.user_id
    db.post.posted_on.default = request.now
    #create form with which the user can submit posts
    crud.settings.formstyle = 'table2cols'
    form = crud.create(db.post)
    #determine who the user follows
    my_followees = db(db.followers.follower==me)
    me_and_my_followees = [me]+[row.followee for row in my_followees.select(db.followers.followee)]
    #Pull all posts to be displayed
    postings = db(db.post.posted_by.belongs(me_and_my_followees)).select(orderby=~db.post.posted_on,limitby=(0,100))
    return locals()


In db.py model

db.define_table('post',
                Field('body', 'text', requires=IS_LENGTH(280, 1), label="What's going down?" ),
                Field('posted_by', 'reference auth_user'),
                Field('posted_on', 'datetime', requires=IS_DATETIME('%d-%m-%Y    %H:%M:%S'))
                )
db.post.body.required = True
db.post.body.requires = IS_LENGTH(280, 1)

db.post.posted_on.required = True
db.post.posted_by.required = True
db.post.posted_on.default = request.now

db.post.posted_on.writable = db.post.posted_on.readable = False
db.post.posted_by.writable = db.post.posted_by.readable = False

My view below
{{extend 'layout.html'}}
 <a class="btn polsearch"   href="{{=URL(c="default", f="search")}}" ><i class="icon-search icon-white spaceit"></i>Search for politicians, people to follow...</a>
{{=A(T("Start a petition"), _href=URL('admin','default','index'), _class='btn petition pull-right',
     _style='margin-top: 1em;')}}
<hr>
{{=form}}
<script>$('textarea').css('width','600px').css('height', '50px');</script>
{{for post in postings:}}
<div style="background:#F0FFFF; margin-bottom:5px; padding:8px; width:600px;">
    <h4>{{=name_of(post.posted_by)}} on {{=post.posted_on}}</h4>
    {{=MARKMIN(post.body)}}
    </div>
{{pass}}

Leonel Câmara

unread,
Aug 11, 2014, 8:33:23 PM8/11/14
to web...@googlegroups.com
 There are a couple of things in your code that stop me from testing this. Where is followers defined, where does me come from? Should there be a line with me = auth.user_id somewhere?  Where is name_of defined ?

After guessing all these things I got it working and could not reproduce your problem:

Controller:

@auth.requires_login()
def timeline():
    me = db.post.posted_by.default =  auth.user_id
    db.post.posted_on.default = request.now
    #create form with which the user can submit posts
    crud.settings.formstyle = 'table2cols'
    form = crud.create(db.post)
    #determine who the user follows
    my_followees = db(db.followers.follower==me)
    me_and_my_followees = [me]+[row.followee for row in my_followees.select(db.followers.followee)]
    #Pull all posts to be displayed
    postings = db(db.post.posted_by.belongs(me_and_my_followees)).select(orderby=~db.post.posted_on,limitby=(0,100))
    return locals()

 
Model:

db.define_table('followers',
                Field('follower', 'reference auth_user'),
                Field('followee', 'reference auth_user'),
                )

db.define_table('post',
                Field('body', 'text', requires=IS_LENGTH(280, 1), label="What's going down?" ),
                Field('posted_by', 'reference auth_user'),
                Field('posted_on', 'datetime', requires=IS_DATETIME('%d-%m-%Y    %H:%M:%S'))
                )
db.post.body.required = True
db.post.body.requires = IS_LENGTH(280, 1)

db.post.posted_on.required = True
db.post.posted_by.required = True
db.post.posted_on.default = request.now

db.post.posted_on.writable = db.post.posted_on.readable = False
db.post.posted_by.writable = db.post.posted_by.readable = False

def name_of(user_id):
    return "%(first_name)s %(last_name)s" % db.auth_user[user_id]  


View:
I'm using the one you provided verbatim


This works without any problem, so I'm not exactly sure if I am guessing something wrong or if you have a weird bug.

As a final note, consider using auth.signature instead of having your own posted_on and posted_by fields.

Reply all
Reply to author
Forward
0 new messages