[web2py] Re: Table reference field instead of the id

2013-12-09 Thread Gael Princivalle
Thanks Massimo but with:
links = [dict(header=T('Brand'), body=lambda row: 
(db.brands(row.brand_id_01).name))]
it give me this error:
type 'exceptions.AttributeError' 'NoneType' object has no attribute 
'name' 

I've also tried with reference:
db.products.brand_id_01.represent = lambda id,row: '%s' % 
db.brands[row.brand_id_01].name()
But error still the same:
type 'exceptions.AttributeError' 'NoneType' object has no attribute 
'name' 

How can I do ?

Best regards.

Il giorno domenica 8 dicembre 2013 23:29:53 UTC+1, Massimo Di Pierro ha 
scritto:

 Try replace

 lambda row: (something that return the brands.name of the 
 products.brand_id_01 current row)

 with

 lambda row: 
 (db.brandshttp://www.google.com/url?q=http%3A%2F%2Fdb.brands.namesa=Dsntz=1usg=AFQjCNE3jQMLe-HFn1TMWcTiEsecaE2E1w
 (row.brand_id_01).name)

 On Sunday, 8 December 2013 11:26:34 UTC-6, Gael Princivalle wrote:

 Thanks Stifan but also with:
 db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, '%(name)s')
 web2py don't display the 
 db.brands.namehttp://www.google.com/url?q=http%3A%2F%2Fdb.brands.namesa=Dsntz=1usg=AFQjCNE3jQMLe-HFn1TMWcTiEsecaE2E1w
  
 instead of the db.products.brand_id_01..

 A solution could be make lambda function, what do you think about 
 something like that ?
 In the controller:
 def products_listing():
 query=db.products
 links = [dict(header=T('Brand'), body=lambda row: (something that 
 return the brands.name of the products.brand_id_01 current row)]
 fields = 
 (db.products.id_01,db.products.code,db.products.description,db.products.brand_id_01)
 grid = SQLFORM.grid(query, fields=fields, links=links)
 return dict(grid=grid)

 Is it possible ?

 Il giorno domenica 8 dicembre 2013 17:10:18 UTC+1, 黄祥 ha scritto:

 i think you should refer it to db.brands.id. e.g.
 db.define_table('brands',
 Field('id_01', unique = True),
 Field('name'),
 format='%(name)s')
 db.define_table('products',
 Field('code', unique=True),
 Field('description'),
 Field('brand_id_01', 'reference brands', requires = 
 IS_IN_DB(db, db.brands.id, '%(name)s')))

 it requires the table products to refer to brands id field not id_01 in 
 table brands. not sure what do you want to achieve in here, i think you can 
 make conditional requires.
 e.g. (not tested)
 if 'crm' in request.function :
 db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, 
 '%(id_01)s')
 else:
 db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, 
 '%(name)s')

 my point is the reference field in products table is refer to the id 
 field on brands, and for the field represented on drop down list is base on 
 the function, if it contain 'crm' it will show the brands.id_01, other 
 function will show the name.

 ref:

 http://web2py.com/books/default/chapter/29/07/forms-and-validators#Database-validators

 http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Record-representation

 best regards,
 stifan



-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Table reference field instead of the id

2013-12-09 Thread Anthony


 I cannot use web2py id's for referencing my brands, I need to use id's 
 from another CRM application called 01. So I have in my brands table a 
 field called id_01 and in the products table a field called 
 brand_id_01.

 

 db.define_table('brands',
 Field('id_01', unique = True),
 Field('name'),
 format='%(names')


Does the brands table in the CRM application have an auto-incrementing 
integer field called id? If not, then the above table definition is 
incorrect. If id_01 is an auto-incrementing integer field, then you 
should do:

db.define_table('brands',
Field('id_01', 'id'),
...)

In that case, your reference field in db.products should work fine (you 
don't even need to bother defining the IS_IN_DB validator).

If id_01 is not an integer field, then you should do:

db.define_table('brands',
Field('id_01', unique=True),
...,
primarykey=['id_01'])

In that case, though, you will only be able to reference db.brands from 
other keyed tables (see 
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Legacy-databases-and-keyed-tables
).

Anthony

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Table reference field instead of the id

2013-12-09 Thread Gael Princivalle
 

With your solution Field('id_01', 'id')now I can add a custom column in the 
grid with links (also without a link inside !):
links = [dict(header=T('Brand'), body=lambda row: 
(db.brands(row.brand_id_01).name))]

Ok, it works fine, but how can I display this custom column in my custom 
single view grid ?
For the description field for example I have:
{{=P(grid.view_form.record.description)}}

For the custom column ?
{{=P(grid.view_form.record.links??)}}


Regards.



Il giorno domenica 8 dicembre 2013 16:44:43 UTC+1, Gael Princivalle ha 
scritto:

 Hello all.

 Well I don't understand really how to manage this problem.

 I cannot use web2py id's for referencing my brands, I need to use id's 
 from another CRM application called 01. So I have in my brands table a 
 field called id_01 and in the products table a field called 
 brand_id_01.
 When I modify a products row in the admin interface I can choose the 
 brand_id_01 by the brands.name, OK, but when I display the products 
 table I see the brand_id_01, KO.
 Also when I display a SQLFORM.grid from the products table I don't see 
 the brands.name instead of the brand_id_01.
 How I can display the brands.name instead of the brand_id_01 in my 
 SQLFORM.grid ?

 My db:
 db.define_table('brands',
 Field('id_01', unique = True),
 Field('name'),
 format='%(names')
 db.define_table('products',
 Field('code', unique=True),
 Field('description'),
 Field('brand_id_01', 'reference brands', requires = 
 IS_IN_DB(db, db.brands.id_01, '%(name)s')))

 My controller:
 def products_listing():
 query=db.products
 fields = 
 (db.products.id_01,db.products.code,db.products.description,db.products.brand_id_01)
 grid = SQLFORM.grid(query, fields=fields)
 return dict(grid=grid)

 Thanks a lot for any kind of help.


-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Table reference field instead of the id

2013-12-09 Thread Gael Princivalle
Well I've done it in another way:
db.define_table('brands',
Field('id_01', 'id'), # 'id' instead of unique = True
Field('name'),
format='%(names')
db.define_table('products',
Field('code', unique=True),
Field('description'),
Field('brand_id_01', 'reference brands', requires = IS_IN_DB
(db, db.brands.id_01, '%(name)s')),
represent=lambda id, r: db.brands[id].name)

And that's ok in the SQLORM.grid web2py display brands.name instead of 
products.brand_id_01.

Now the problem is in my custom single record grid view, web2py still 
display products.brand_id_01.
{{=P(T('Brand'), : , grid.view_form.record.brand_id_01)}}

Someone's knows why ?

Bye.

Il giorno lunedì 9 dicembre 2013 18:38:52 UTC+1, Gael Princivalle ha 
scritto:

 With your solution Field('id_01', 'id')now I can add a custom column in 
 the grid with links (also without a link inside !):
 links = [dict(header=T('Brand'), body=lambda row: 
 (db.brands(row.brand_id_01).name))]

 Ok, it works fine, but how can I display this custom column in my custom 
 single view grid ?
 For the description field for example I have:
 {{=P(grid.view_form.record.description)}}

 For the custom column ?
 {{=P(grid.view_form.record.links??)}}


 Regards.



 Il giorno domenica 8 dicembre 2013 16:44:43 UTC+1, Gael Princivalle ha 
 scritto:

 Hello all.

 Well I don't understand really how to manage this problem.

 I cannot use web2py id's for referencing my brands, I need to use id's 
 from another CRM application called 01. So I have in my brands table a 
 field called id_01 and in the products table a field called 
 brand_id_01.
 When I modify a products row in the admin interface I can choose the 
 brand_id_01 by the brands.name, OK, but when I display the 
 products table I see the brand_id_01, KO.
 Also when I display a SQLFORM.grid from the products table I don't see 
 the brands.name instead of the brand_id_01.
 How I can display the brands.name instead of the brand_id_01 in my 
 SQLFORM.grid ?

 My db:
 db.define_table('brands',
 Field('id_01', unique = True),
 Field('name'),
 format='%(names')
 db.define_table('products',
 Field('code', unique=True),
 Field('description'),
 Field('brand_id_01', 'reference brands', requires = 
 IS_IN_DB(db, db.brands.id_01, '%(name)s')))

 My controller:
 def products_listing():
 query=db.products
 fields = 
 (db.products.id_01,db.products.code,db.products.description,db.products.brand_id_01)
 grid = SQLFORM.grid(query, fields=fields)
 return dict(grid=grid)

 Thanks a lot for any kind of help.



-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Table reference field instead of the id

2013-12-08 Thread 黄祥
i think you should refer it to db.brands.id. e.g.
db.define_table('brands',
Field('id_01', unique = True),
Field('name'),
format='%(name)s')
db.define_table('products',
Field('code', unique=True),
Field('description'),
Field('brand_id_01', 'reference brands', requires = 
IS_IN_DB(db, db.brands.id, '%(name)s')))

it requires the table products to refer to brands id field not id_01 in 
table brands. not sure what do you want to achieve in here, i think you can 
make conditional requires.
e.g. (not tested)
if 'crm' in request.function :
db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, 
'%(id_01)s')
else:
db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, 
'%(name)s')

my point is the reference field in products table is refer to the id field 
on brands, and for the field represented on drop down list is base on the 
function, if it contain 'crm' it will show the brands.id_01, other function 
will show the name.

ref:
http://web2py.com/books/default/chapter/29/07/forms-and-validators#Database-validators
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Record-representation

best regards,
stifan

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Table reference field instead of the id

2013-12-08 Thread Gael Princivalle
Thanks Stifan but also with:
db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, '%(name)s')
web2py don't display the db.brands.name instead of the 
db.products.brand_id_01..

A solution could be make lambda function, what do you think about something 
like that ?
In the controller:
def products_listing():
query=db.products
links = [dict(header=T('Brand'), body=lambda row: (something that 
return the brands.name of the products.brand_id_01 current row)]
fields = 
(db.products.id_01,db.products.code,db.products.description,db.products.brand_id_01)
grid = SQLFORM.grid(query, fields=fields, links=links)
return dict(grid=grid)

Is it possible ?

Il giorno domenica 8 dicembre 2013 17:10:18 UTC+1, 黄祥 ha scritto:

 i think you should refer it to db.brands.id. e.g.
 db.define_table('brands',
 Field('id_01', unique = True),
 Field('name'),
 format='%(name)s')
 db.define_table('products',
 Field('code', unique=True),
 Field('description'),
 Field('brand_id_01', 'reference brands', requires = 
 IS_IN_DB(db, db.brands.id, '%(name)s')))

 it requires the table products to refer to brands id field not id_01 in 
 table brands. not sure what do you want to achieve in here, i think you can 
 make conditional requires.
 e.g. (not tested)
 if 'crm' in request.function :
 db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, 
 '%(id_01)s')
 else:
 db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, 
 '%(name)s')

 my point is the reference field in products table is refer to the id field 
 on brands, and for the field represented on drop down list is base on the 
 function, if it contain 'crm' it will show the brands.id_01, other function 
 will show the name.

 ref:

 http://web2py.com/books/default/chapter/29/07/forms-and-validators#Database-validators

 http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Record-representation

 best regards,
 stifan


-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Table reference field instead of the id

2013-12-08 Thread Massimo Di Pierro
Try replace

lambda row: (something that return the brands.name of the 
products.brand_id_01 current row)

with

lambda row: 
(db.brandshttp://www.google.com/url?q=http%3A%2F%2Fdb.brands.namesa=Dsntz=1usg=AFQjCNE3jQMLe-HFn1TMWcTiEsecaE2E1w
(row.brand_id_01).name)

On Sunday, 8 December 2013 11:26:34 UTC-6, Gael Princivalle wrote:

 Thanks Stifan but also with:
 db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, '%(name)s')
 web2py don't display the 
 db.brands.namehttp://www.google.com/url?q=http%3A%2F%2Fdb.brands.namesa=Dsntz=1usg=AFQjCNE3jQMLe-HFn1TMWcTiEsecaE2E1w
  
 instead of the db.products.brand_id_01..

 A solution could be make lambda function, what do you think about 
 something like that ?
 In the controller:
 def products_listing():
 query=db.products
 links = [dict(header=T('Brand'), body=lambda row: (something that 
 return the brands.name of the products.brand_id_01 current row)]
 fields = 
 (db.products.id_01,db.products.code,db.products.description,db.products.brand_id_01)
 grid = SQLFORM.grid(query, fields=fields, links=links)
 return dict(grid=grid)

 Is it possible ?

 Il giorno domenica 8 dicembre 2013 17:10:18 UTC+1, 黄祥 ha scritto:

 i think you should refer it to db.brands.id. e.g.
 db.define_table('brands',
 Field('id_01', unique = True),
 Field('name'),
 format='%(name)s')
 db.define_table('products',
 Field('code', unique=True),
 Field('description'),
 Field('brand_id_01', 'reference brands', requires = 
 IS_IN_DB(db, db.brands.id, '%(name)s')))

 it requires the table products to refer to brands id field not id_01 in 
 table brands. not sure what do you want to achieve in here, i think you can 
 make conditional requires.
 e.g. (not tested)
 if 'crm' in request.function :
 db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, 
 '%(id_01)s')
 else:
 db.products.brand_id_01.requires = IS_IN_DB(db, db.brands.id, 
 '%(name)s')

 my point is the reference field in products table is refer to the id 
 field on brands, and for the field represented on drop down list is base on 
 the function, if it contain 'crm' it will show the brands.id_01, other 
 function will show the name.

 ref:

 http://web2py.com/books/default/chapter/29/07/forms-and-validators#Database-validators

 http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Record-representation

 best regards,
 stifan



-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.