On 01/25/2012 01:57 PM, Tim Black wrote:
> My *model* is like this:
>
> __init__.py:
>
> from projects.model.auth import User
> from projects.model.main import Company
>
> auth.py:
>
> class User(DeclarativeBase):
>     company_id = Column('company_id', Integer,
> ForeignKey('company.id')) # many-to-one
>
> main.py:
>
> class Company(DeclarativeBase):
>     id = Column(Integer, primary_key=True)
>     name = Column(Unicode(200), nullable=False)
>     users = relation('User') # one-to-many
>
> My *template* contains:
>
> ${user.company.name}
>
> This throws the following error:
>
> UndefinedError: <User...> has no member named "company"
>
> I know User.company doesn't exist.  But how can I make it exist in the
> model so that ${user.company} returns a Company object rather than
> just a primary key value from the database?  In other words, what is
> the right syntax to get ${user.company.name} to display the company
> name in the template?  I've tried variations of backref=... but
> haven't been able to get it right.
Sigh...the following simple change (in bold) fixed the problem.

main.py:

class Company(DeclarativeBase):
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(200), nullable=False)
    users = relation('User'*, backref='company'*) # one-to-many

Tim

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to