Now that I've spent a little more time implementing your answer, I've
noticed that the answers are sorted by id. Is there any way to take the
table object and sort it? I've already tried .sorted(), but as expected,
it didn't work. This seems like the sort of thing you'd do in a
controller, so I can understand why you wouldn't be able to do this in
db.py.
On Thursday, January 30, 2014 10:36:55 AM UTC-8, Anthony wrote:
>
> On Tuesday, January 28, 2014 2:13:27 PM UTC-5, Ryan Matlock wrote:
>>
>> It appears that there are two ways to get a field to reference another
>> table,
>> Field("my_field", requires=IS_IN_DB(db, db.some_table))
>> and
>> Field("my_field_2", db.some_table_2)
>>
>
> Note, these are *not* two different ways to get a field to reference
> another table. Only the second method creates an actual "reference" field
> type. You'll notice it behaves similarly to the first in forms because the
> second implicitly gets an IS_IN_DB validator, just like the first. You
> should definitely use the second method for reference fields.
>
>
>> The problem with both of these (and especially the first one), is that I
>> can't seem to get the format for my_field to grab the format of some_table
>> and my_field_2 can't grab the format from tables that some_table_2 uses in
>> its format.
>>
>
> When you do Field('myfield', 'reference othertable'), the field gets a
> default validator like IS_IN_DB(db, db.othertable._id,
> db.othertable._format), and it gets a default "represent" attribute like
> lambda id, r: db.other_table._format % db.other_table(id).
>
> Note, the _format attributes do not propagate through multiple tables. So,
> if your test_model table has:
>
> format = "%(model_year)s %(manufacturer)s %(name)s"
>
> the %(manufacturer)s part of the format attribute will just refer to the
> actual integer ID value stored in the db.test_model.manufacturer field. It
> will not look up the _format attribute of the db.manufacturer table and
> retrieve the db.manufacturer.name value. To do the latter, you have to be
> explicit:
>
> format = lambda r: '%s %s %s' % (r.model_year, r.manufacturer.name, r.name
> )
>
> The _format attribute can be a function that takes a row from the table
> and returns whatever you want. In the above, r.manufacturer.name is a
> recursive select -- it selects the record from db.manufacturer with
> id==r.manufacturer, and then extracts the "name" field value from that
> record.
>
> Be careful about including recursive selects in the _format attribute
> (which will then be used to construct the IS_IN_DB validator and
> "represent" attributes of fields that reference the table). There will be a
> separate recursive select for every record (e.g., every item in the
> drop-down created by the IS_IN_DB validator in forms, and every row
> displayed in a SQLFORM.grid). This can be quite inefficient if there are
> many records. An alternative is to use an IS_IN_SET validator instead, and
> construct the options by doing a multi-table join query (probably a good
> idea to cache the results as well).
>
> 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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.