On Sep 11, 2006, at 8:23 AM, dmiller wrote:

>
> On Sep 9, 2006, at 10:11 AM, Aaron Spike wrote:
>> I've got a one-to-one relationship something like this.
>>
>> class Person(object):
>>     pass
>> class Photo(object):
>>     pass
>>
>> persons_table = Table('persons', metadata,
>>     Column('id', Integer, primary_key=True),
>>     Column('firstname', String(255),
>>     Column('lastname', String(255),
>>     ...
>>     )
>> photos_table = Table('photos', metadata,
>>     Column('person_id', Integer, primary_key=True),
>>     Column('photo',),
>>     ForeignKeyConstraint(['person_id'],
>>         ['persons.id'],'persons_photos_fk')
>>     )
>>
>> photomapper = mapper(Photo, photos_table)
>> personmapper = mapper(Person, persons_table,
>>     properties={'photo':relation(Photo, uselist=False)})
>>
>> And now to access a person's photo I have to do
>> "person.photo.photo". Is
>> there anyway I can do "person.photo" instead and still have it lazy
>> load
>> the photo? I could make a getPhoto() method on the class but is a
>> property possible?
>
>
> You might consider doing it like this:
>
>
> class Person(object):
>      def _get_photo(self):
>          if self._photo is None:
>              return None
>          return self._photo.photo
>      def _set_photo(self = photo):
-------------------------^^
Typo... should be:

       def _set_photo(self, photo):

>          if self._photo is None:
>              self._photo = Photo()
>          self._photo.photo = photo
>      photo = property(_get_photo, _set_photo)
>
> ...
>
> personmapper = mapper(Person, persons_table,
>      properties={'_photo':relation(Photo), uselist=False)})
>
>
> That way you can use a normal lazy relationship, and can even access
> the '_photo' attribute directly if you need to.
>

That's obviously untested code. :)

~ Daniel




-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to