Form validators: IS_IN_DB(..., _or=other_validator) ?

64 views
Skip to first unread message

Louis Amon

unread,
May 23, 2014, 4:52:58 PM5/23/14
to web...@googlegroups.com
Due to external constraints, I need to define two tables that are actually siblings and a third table's field that can choose from any of said tables.

e.g.:
db.define_table('melon')
db.define_table('watermelon')

I need the third table's field to be a reference to either of the two first tables.

Is there a way to define this in terms of field type and validators ?
I was thinking along the lines of:

db.define_table('my_table', Field('fruit_id', type=???, requires=IS_IN_DB(db, db.melon, _or=IS_IN_DB(db, db.watermelon)))

or maybe:

db.define_table('my_table', Field('fruit_id', type=???, requires=any([IS_IN_DB(db, db.melon), IS_IN_DB(db, db.watermelon)]))

But of course it doesn't work...


How can I work around this issue ?

Anthony

unread,
May 23, 2014, 5:03:10 PM5/23/14
to web...@googlegroups.com
How would you know whether the field stores an id of the melon table or the watermelon table?

Louis Amon

unread,
May 23, 2014, 5:24:49 PM5/23/14
to web...@googlegroups.com
Well in this particular case I needn't know wether I'm storing a melon or a watermelon's id.

I know it's kind of idiotic but I just need a 'fruit' id.

Think of it as a subclassing matter : the API I'm connecting to is doing something along the lines of subclassing anyway, and I need to store data about every transaction my program does with this poorly-designed API
--
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/nd6ED9oScsA/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.

Anthony

unread,
May 23, 2014, 5:39:33 PM5/23/14
to web...@googlegroups.com
I suppose you could just make it type='integer'. Then you could use the IS_IN_SET validator and construct a list of id's from the two tables (keeping in mind you should de-dup the list, as both tables will have many of the same id's). Of course, if you end up storing a value of say, 5, you won't know if it is referencing record #5 in the melon table or record #5 in the watermelon table.

Anthony
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Louis Amon

unread,
May 25, 2014, 4:41:25 AM5/25/14
to web...@googlegroups.com
It did the trick, thanks Anthony!

By the way the difficult part for me was building the list containing all ids from multiple tables.
Assuming ids are unique across tables (which was my case), this function can help build the IS_IN_SET validator :

def mix(list_long, list_short):
    i,j = iter(list_long), iter(list_short)
    result = [item for sublist in zip(i,j) for item in sublist]
    result += [item for item in i]
    result += [item for item in j]
    return result
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages