Given the field definition:
Field('company_name', 'reference company', notnull=True)
and the fact that the db.company table has a "format" attribute, you will
get a default "represent" attribute for the db.project.company_name field
that displays the company name rather than its record ID. However, the
represent attribute is used only in SQLTABLE, SQLFORM.grid, and read-only
SQLFORM fields, not anywhere you happen to display the value of the field.
So, you have to be explicit:
{{# Explicitly pass the value to the field's "represent" function.}}
{{=db.project.company_name.represent(item[3])}}
or
{{# Query the db.company table for the item[3] record and extract the
company_name field.}}
{{=db.company(item[3]).company_name}}
In trunk, there is also now a new method that would work if you had a Rows
object instead of using executesql:
def_index:
items = db().select(db.project.company_name)
return locals()
{{for item in items.render():}}
{{=item.company_name}}
{{pass}}
items.render() returns an iterable that generates a copy of each Row object
with each field transformed by its "represent" attribute. You can also
extract just a specific Row:
items.render(0)
As an aside, it would be more common to name the db.project.company_name
field either just db.project.company or db.project.company_id. It is
misleading to name it db.project.company_name, as it doesn't actually store
the name (it is merely represented by the name in some display contexts).
Finally, any reason for using executesql rather than a DAL select?
Anthony
On Saturday, August 31, 2013 11:28:40 PM UTC-7, אבי אברמוביץ wrote:
>
> index.html:
> {{extend 'layout.html'}}
> <h2>All projects:</h2>
> <br/>
> {{for item in items: }}
> {{=item[3]}}
> {{pass}}
>
> def_index:
> items = db.executesql('SELECT * FROM project;')
> return locals()
>
> model:
>
> db.define_table('company',
> Field('company_name', notnull=True, unique=True),
> Field('email'),
> Field('phone', notnull=True),
> Field('url'),
> format = '%(company_name)s')
> db.company.email.requires=IS_EMAIL()
> db.company.url.requires=IS_EMPTY_OR(IS_URL())
>
> db.define_table('project',
> Field('name', notnull=True),
> Field('employee_name', db.auth_user,
> default=auth.user_id),
> Field('company_name', 'reference company', notnull=True),
> Field('description', 'text', notnull=True),
> Field('start_date', 'date', notnull=True),
> Field('due_date', 'date', notnull=True),
> Field('completed', 'boolean', notnull=True),
> format = '%(company_name)s')
> db.project.employee_name.readable = True
> db.project.employee_name.writable = False
> db.project.start_date.requires = IS_DATE(format=T('%m-%d-%Y'),
> error_message='Must be MM-DD-YYYY!')
> db.project.due_date.requires = IS_DATE(format=T('%m-%d-%Y'),
> error_message='Must be MM-DD-YYYY!')
>
>
>
>
>
>
>
>
> On Sunday, September 1, 2013 2:56:47 AM UTC+3, Massimo Di Pierro wrote:
>>
>> I need to see more code.
>>
>> On Saturday, 31 August 2013 16:25:32 UTC-5, אבי אברמוביץ wrote:
>>>
>>> Hi,
>>> I do this query:
>>> items = db.executesql('SELECT * FROM project;')
>>> For the field below, which is a field in the db.project:
>>> Field('company_name', 'reference company', notnull=True).
>>> On the view, "{{=item[3]}}" renders only '1'.(instead of the company
>>> name).
>>> What should I do?
>>> Thanks.
>>>
>>
--
---
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.