create a link SQLFORM to another table with id.belongs

187 views
Skip to first unread message

LoveWeb2py

unread,
Jun 5, 2014, 7:21:27 PM6/5/14
to web...@googlegroups.com
I have two tables:

table1 & table 2

I compare table 2 events against table 1 and if it matches I insert it into table 2's field called "table2.table1_id"

table2.table1_id has thousands of rows that have the row id of table 1 in it in the form of a list.

Normally I could use the belongs feature, but I'm having a hard time getting it to work in the form of a link. Any thoughts on how I could do this?


Fabiano Almeida

unread,
Jun 5, 2014, 7:41:51 PM6/5/14
to web...@googlegroups.com
I use this:

grid for table1
def index():
    return dict(grid=SQLFORM.grid(Almoxarifado, links = [lambda row: A('Listar Objetos',_href=URL('objeto','index',args=[row.id]))], user_signature=False, csv=False))

grid for table2
def index():
    if (not request.args)  and (not request.vars): redirect(URL('almoxarifado','index'))
    alm = None
    if request.args: alm = request.args(0)
    try:
        if alm:
            alm = int(alm)
            session.almoxarifado=alm
            session.nomealmox = Almoxarifado[alm].nome
    except ValueError:
        pass
    Objeto.almoxarifado_id.default = session.almoxarifado
    form = SQLFORM.grid(Objeto.almoxarifado_id == session.almoxarifado, fields=[Objeto.nome, Objeto.quantidade, Objeto.localizacao], user_signature=False, csv=False)
    return dict(form=form, nome=session.nomealmox)

LoveWeb2py

unread,
Jun 5, 2014, 8:42:44 PM6/5/14
to web...@googlegroups.com
This doesn't seem to work for me Fabiano,

When I click the button it just keeps table1 on the screen.  There has got to be an easier way (I hope)

LoveWeb2py

unread,
Jun 6, 2014, 11:45:33 AM6/6/14
to web...@googlegroups.com
 SQLFORM isn't working for me with


new_table = db(db.table1.id.belongs(record_ids)).select()
if I do {{=new_table}} in my view I can see the records which belong to record_ids, but if I do:

SQLFORM.grid(new_table)
return dict(grid=grid)


I get an error 'Rows' object has no attribute '_db'

Fabiano Almeida

unread,
Jun 6, 2014, 12:02:05 PM6/6/14
to web...@googlegroups.com
Hi,

You send var grid (see: return dict(grid=grid)). The first var grid is a send var to your view, de second var grid is a local var of your function.

Then, 
in controller use:
grid = SQLFORM.grid(db.new_table)
return dict(grid=grid)

in the view use: 
{{=grid}}

Fabiano.


--
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 the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

LoveWeb2py

unread,
Jun 6, 2014, 12:10:34 PM6/6/14
to web...@googlegroups.com
Hi Fabiano,

I actually was already using grid= SQLFORM.grid(db.new_table). That is when I get the error.

If I do grid='' to test it and just return the table without SQLFORM I can see it in the view, but when I apply SQLFORM I get the error 'Rows' object has no attribute '_db'.

I'm guessing this is because of the way SQLFORM handles the grid? Could there be something in  my model messing it up?

Fabiano Almeida

unread,
Jun 6, 2014, 1:03:35 PM6/6/14
to web...@googlegroups.com
Try:

grid = SQLFORM.grid(db.table,  user_signature=False)

Fabiano Almeida

unread,
Jun 6, 2014, 1:06:46 PM6/6/14
to web...@googlegroups.com
How do you define tables?

LoveWeb2py

unread,
Jun 6, 2014, 1:07:59 PM6/6/14
to web...@googlegroups.com
That was it! THANK YOU! Why does it work without a signature though?

Fabiano Almeida

unread,
Jun 6, 2014, 1:09:59 PM6/6/14
to web...@googlegroups.com
Are you logged in your app?

Austin Taylor

unread,
Jun 6, 2014, 1:17:39 PM6/6/14
to web...@googlegroups.com
yes


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/I8aGvbVcQxk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

LoveWeb2py

unread,
Jun 6, 2014, 2:18:57 PM6/6/14
to web...@googlegroups.com
Now I get this error when I try to click edit on the query database:

<type 'exceptions.ValueError'> invalid literal for int() with base 10: 'edit'

I've tried changing signature to false, but I don't think thats the problem.


On Friday, June 6, 2014 1:17:39 PM UTC-4, LoveWeb2py wrote:
yes


On Fri, Jun 6, 2014 at 1:09 PM, Fabiano Almeida <fab...@techno7.com.br> wrote:
Are you logged in your app?
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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/I8aGvbVcQxk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Fabiano Almeida

unread,
Jun 6, 2014, 2:30:07 PM6/6/14
to web...@googlegroups.com
Show your code

Fabiano Almeida

unread,
Jun 6, 2014, 3:43:12 PM6/6/14
to web...@googlegroups.com
<type 'exceptions.ValueError'> invalid literal for int() with base 10: 'edit'

In some part of your code has converting string to numeric type. Probably you should be picking up the URL argument and doing the conversion, but when you edit the registry of URL arguments are changed. 

In my example, I use session and try...except to resolve this.

LoveWeb2py

unread,
Jun 9, 2014, 12:47:44 PM6/9/14
to web...@googlegroups.com
Hi Fabiano,

Thank you for your reply. What do you mean by "edit the registry of URL arguments are changed. "

Here is my code:
def query_table2():
   table2_records = []
   table1_inv_record = request.args(0)
   row = db(db.table1.id==table1_inv_record).select()
   for line in row:
      joined_records = line.inv_id
   query=db.table1.id.belongs(joined_records)
   grid=SQLFORM.grid(query, user_signature=False)
   return dict(grid=grid)

view for query_table2.html:
   {{=grid}}
Show your code


yes


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/I8aGvbVcQxk/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.

Fabiano Almeida

unread,
Jun 9, 2014, 1:30:49 PM6/9/14
to web...@googlegroups.com
Hi,

Sorry for my poor english, by lazy, sometimes I use google translate (portuguese -> english), and don't check the translation.

table1_inv_record = request.args(0)
In this line you get de first args, but on click to edit record in grid, the function it's called again and url change args, then first args in url is 'edit', and this is literal, not numerical.

row = db(db.table1.id==table1_inv_record).select()
In edit mode (and changed url), get error because var table1_inv_record is not numerical at this time. I think this is a line in your traceback error.

So I use try...except block to convert first arg in integer, if ok, use session to store de right id sent in the original url.
alm = None # this is for prevent error case haven't args(0)

if request.args: alm = request.args(0)
  try:
      if alm:
          alm = int(alm)
          session.almoxarifado=alm    # this line executes only if the previous line no error

  except ValueError:
      pass
Objeto.almoxarifado_id.default = session.almoxarifado

LoveWeb2py

unread,
Jun 9, 2014, 1:48:32 PM6/9/14
to web...@googlegroups.com
No problem at all and I appreciate you trying to respond to English.

Is Object your table name and Almoxarifado_id is your id field in Objeto?

Objeto.almoxarifado_id

Fabiano Almeida

unread,
Jun 9, 2014, 1:53:21 PM6/9/14
to web...@googlegroups.com
Objeto it's the table

almoxarifado_id it's a FK (reference field)

LoveWeb2py

unread,
Jun 9, 2014, 4:09:18 PM6/9/14
to web...@googlegroups.com
Hi Fabiano,

I'm still trying to get this

For your Objeto.almoxarifado_id.default = session.almoxarifado

Shouldn't there be a db.Objeto.almoxarifado_id ? If I try to just put table2.id I get an error saying table2 isn't defined.

Here is what I have so far

def query_table2():
   table1_inv_record = None

   table2_records = []
   table1_inv_record = request.args(0)
   row = db(db.table1.id==table1_inv_record).select()
   for line in row:
      joined_records = line.inv_id
   if table1_inv_record:
     inv1_record = int(table1_inv_record)
     session.table1_id = inv1_record
   query=db.table1.id.belongs(joined_records)
   db.table1.id.default = session.table1.id
   grid=SQLFORM.grid(query, user_signature=False)
   return dict(grid=grid)

Still getting the same error though

My db.model is like this: db.define_table('table2', Field('table1_inv_record', 'list:integer')

Fabiano Almeida

unread,
Jun 9, 2014, 4:32:54 PM6/9/14
to web...@googlegroups.com
def query_table2():
   table1_inv_record = None
   table2_records = []
   if request.args:
      try:
         session.table1_id = int(request.args(0))
      except:
         pass
   row = db(db.table1.id==session.table1_id).select()
   for line in row:
      joined_records = line.inv_id
   query=db.table1.id.belongs(joined_records)
   db.table1.id.default = session.table1_id
   grid=SQLFORM.grid(query, user_signature=False)
   return dict(grid=grid)


Fabiano Almeida

unread,
Jun 9, 2014, 4:38:04 PM6/9/14
to web...@googlegroups.com
My db.py:


Almoxarifado = db.define_table('almoxarifado', Field('name'))

Objeto = db.define_table('objeto', Field('name'), Field('almoxarifado_id', db.almoxarifado))
Objeto.almoxarifado_id.requires=IS_IN_DB(db, 'almoxarifado.id', '%(nome)s')



2014-06-09 17:09 GMT-03:00 LoveWeb2py <atayl...@gmail.com>:

LoveWeb2py

unread,
Jun 9, 2014, 4:52:20 PM6/9/14
to web...@googlegroups.com
It's working! Thank you so much! I actually didn't need the db.table1.id.default = session.table1.id

I think this was the line that fixed it:
row = db(db.table1.id==session.table1_id).select()

Thank you, thank you, thank you!! Such a huge help. Have a virtual beer on me :)

Once

Fabiano Almeida

unread,
Jun 9, 2014, 6:40:08 PM6/9/14
to web...@googlegroups.com
Hi,

Good!

db.table1.id.default = session.table1.id

This line is used to insert new record automatically by SQLFORM. For the id field is unnecessary, it is useful when you want to set a default value, eg FK.

All beers!!


Reply all
Reply to author
Forward
0 new messages