lundi 30 mars 2015

Reference another table in SQLite using Web2py/Python

I'm new to Web2py and SQL Databases in general, so forgive me if this is a dumb question.


What I'm trying to do is create a form that pulls relevant data (type, label, IS_NOT_EMPTY(), etc.) from an SQL table's field to populate and configure input elements on the frontend. So far I've successfully been able to do this using the Web2py Manual as an excellent reference.


I've gotten to the point where I'd like to use multiple tables to help categorize sets of data more effectively. Best case scenario would be to have one master table that can be called in my controller file (default.py), that is linked with other relevant (but not always needed) tables in my db.


In the below code you can see that I define the 'category_t' table first, then define the 'new_product' table. I then attempt to reference 'category_t' table within the 'main_category' field on new_product. The 'main_category' field ends up being a multiple select element on the form, which is exactly what I want. I just need to use the field labels from 'category_t' as the multi-select options.


I was using this Links to referencing records instructions from the Web2py Manual, but I believe I'm misinterpreting what it's actually capable of. I desperately need someone to school me on the laws of Web2py and SQLite.



db.define_table(
'category_t',
Field('category1', 'boolean', label='Category 1'),
Field('category2', 'boolean', label='Category 2'),
Field('category3', 'boolean', label='Category 3'),
)

db.define_table(
'new_product',
Field('name', requires=[IS_NOT_EMPTY(), IS_ALPHANUMERIC()], label='Product Name'),
Field('sku', requires=[IS_NOT_EMPTY(), IS_ALPHANUMERIC()], label='SKU'),
Field('build', requires=IS_IN_SET(['Build1','Build2','Build3']), label='Product Type'),
Field('main_category', 'list:reference category_t', label='Category'),
)

Aucun commentaire:

Enregistrer un commentaire