self-reference not showing dropdown on form field

262 views
Skip to first unread message

黄祥

unread,
Mar 3, 2013, 8:03:02 AM3/3/13
to web...@googlegroups.com
hi,

i want to create category and sub category table using self-reference. when i create the table it's seems normal (i've checked in sql.log) :
CREATE TABLE category_archive(
id INTEGER PRIMARY KEY AUTOINCREMENT,
category CHAR(10) NOT NULL UNIQUE,
parent INTEGER REFERENCES category (id) ON DELETE CASCADE,
is_active CHAR(1),
created_on TIMESTAMP,
created_by INTEGER REFERENCES auth_user (id) ON DELETE CASCADE,
modified_on TIMESTAMP,
modified_by INTEGER REFERENCES auth_user (id) ON DELETE CASCADE,
current_record INTEGER REFERENCES category (id) ON DELETE CASCADE
);
and here is in model : db.py 
db.define_table('category',
    Field('category', length=10, notnull=True, unique=True),
    Field('parent', 'reference category'),
    format='%(category)s')
the problem is when i want to fill the form the parent field is not showing drop down, even in database administration too, is it normal or there is something wrong in my code?

note:
i've tried the others table that using reference to another table it's work fine (showing drop down in form field)
i've tried to copas from book http://web2py.com/books/default/chapter/29/06#Self-Reference-and-aliases the result is same too (not showing drop down in form field even in database administration too)

thank you very much in advance

Anthony

unread,
Mar 3, 2013, 1:14:31 PM3/3/13
to web...@googlegroups.com
Self-reference fields don't get the automatic "requires" and "represent" attributes defined because the referenced table does not yet exist at the time the field is defined (because the field is contained within the table definition, which obviously hasn't completed yet). So, you'll have to manually define the requires and represent attributes after you define the table.

Anthony

黄祥

unread,
Mar 3, 2013, 9:34:59 PM3/3/13
to web...@googlegroups.com
a, thank you so much for your detail explaination anthony. it's work fine now by add :

db.category.parent.requires=IS_IN_DB(db, 'category.category')

btw, with this, i still have a problem for input the parent category. because in form there is not allowed to put an empty value. i have 2 ideas for this problem :
1. put the parent category directly to database via database input command
2. create the root category above parent category :
for example
root category
|
parent category
child category

do you have better idea or suggestion about this?

thanks a lot before

Anthony

unread,
Mar 4, 2013, 8:34:14 AM3/4/13
to web...@googlegroups.com
db.category.parent.requires = IS_EMPTY_OR(IS_IN_DB(db, 'category.category'))

Richard Vézina

unread,
Mar 4, 2013, 9:07:35 AM3/4/13
to web2py-users
There is a issue open for this by me and Massimo try a couple of time to solve it, but it seems it still there...

Richard


--
 
---
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/groups/opt_out.
 
 

Richard Vézina

unread,
Mar 4, 2013, 9:08:22 AM3/4/13
to web2py-users

Anthony

unread,
Mar 4, 2013, 11:30:15 AM3/4/13
to web...@googlegroups.com
This is not a list:reference field, just a regular reference.

Richard Vézina

unread,
Mar 4, 2013, 11:39:00 AM3/4/13
to web2py-users
Yeah I just read the code, I read self-reference in his question and you answer first word was Self-reference :)

Richard

黄祥

unread,
Mar 4, 2013, 4:40:52 PM3/4/13
to web...@googlegroups.com

db.category.parent.requires = IS_EMPTY_OR(IS_IN_DB(db, 'category.category'))

a, the solution is so simple just to add the validator : IS_EMPTY_OR(). web2py is so efficient in developing time. thank you very much anthony.

Isaac Batista

unread,
May 15, 2019, 1:28:54 AM5/15/19
to web2py-users
thxs, same problem. it has worked for me
Reply all
Reply to author
Forward
0 new messages