[web2py] Re: problem with list:reference

2020-07-20 Thread villas
Your model deletes the records and recreates them every time the model 
runs.  This is not such a good idea for testing.  
Furthermore, it seems to retain the first product,  but deletes the 
referenced keys.
Your table ids are not reset to zero,  so your first product will probably 
have missing keys for tag.

Anyhow, here are two suggestions:
You can use truncate() rather than delete to empty the tables.
Only insert the test records when the database is empty.

So insert these lines under your table define statements.  Run model once 
and then comment out:
db.product.truncate()
db.tag.truncate()

Afterwards use the other code to recreate the records only once:
if db(db.product.id>0).count() == 0:
a = db.tag.insert(name='red')
b = db.tag.insert(name='green')
c = db.tag.insert(name='blue')
db.product.insert(name='Toy Car', tags=[a, b, c])
db.product.insert(name='Toy Boat', tags=[a])


On Sunday, 19 July 2020 01:46:53 UTC+1, P Page-McCaw wrote:
>
> I am having trouble getting list:reference to work. I have made a tester 
> app where db.py is:
>
> db.define_table('tag',
> Field('name'),
> format='%(name)s')
>
> db.define_table('product',
>  Field('name'),
>  Field('tags', 'list:reference tag'))
>
> #this isn't necessary for the error, but things were getting out of hand
> if db(db.product.id>0).count() != 0:
> db(db.tag.id > 0).delete()
> db(db.product.id > 1).delete()
> 
> a = db.tag.insert(name='red')
> b = db.tag.insert(name='green')
> c = db.tag.insert(name='blue')
> db.product.insert(name='Toy Car', tags=[a, b, c])
> db.product.insert(name='Toy Boat', tags=[a])
>
> That part all seems to work and I get a correct database when I look in 
> the Admin interface. This is from the manual. 
> Then in the Database Administration interface, I create a new Product "Toy 
> Truck" and select say "Green". But when I click submit, I get "Value not in 
> database". If I try to add a new color to tag, the tag does not appear in 
> the selection field when I then make a new product. I have similar problems 
> in my own views, but figure if I can't get this...
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/db7b89b4-28d8-411e-9ca1-c63371c04009o%40googlegroups.com.


[web2py] Re: Problem with list:reference

2011-03-17 Thread goutham v
thanks for the solution it worked.
but in doc 
herehttp://web2py.com/book/default/chapter/06#Many-to-Many,-list:type,-and-contains
 it 
asks to use the queried object directly?
doesn't that way work?


[web2py] Re: Problem with list:reference

2011-03-17 Thread Massimo Di Pierro
You can use a nested select but it requires _select(db.table.field).
You cannot pass a record as returned by .first()

On Mar 17, 11:26 am, goutham v goutham...@gmail.com wrote:
 thanks for the solution it worked.
 but in doc 
 herehttp://web2py.com/book/default/chapter/06#Many-to-Many,-list:type,-and-contains
  it
 asks to use the queried object directly?
 doesn't that way work?


[web2py] Re: Problem with list:reference

2011-03-16 Thread Massimo Di Pierro
Should be

tag = db(db.tags.name=='testtag').select().first()
q = db.branch.tags.contains(tag.id)
branch = db(q).select().first()

On Mar 16, 2:40 pm, goutham goutham...@gmail.com wrote:
 = my model ==
 db.define_table('tags',
     Field('name','string', unique= True),
     format='%(name)s')

 db.define_table('branch',
     Field('name','string'),
     Field('users', 'list:reference auth_user'),
     Field('tags','list:reference tags'),
     format='%(name)s')

 === my controller ===
 tag = db(db.tags.name=='testtag').select().first()
 q = db.branch.tags.contains(tag)
 branch = db(q).select().first()

 after performing this operation the value in branch is None  I
 have inserted proper corresponding values in db using dbadmin.

 why arn't the query working ?