Re: [sqlalchemy] hybrid, relationships and inheritance.

2011-07-11 Thread James Studdart

Hi Michael,
 thanks for your reply. I did get it working with the 
@property/hybrid_property, but it turns out what I really needed was a 
custom collection class. At least, that seems to be working in a cleaner 
fashion.


I'm still fumbling around in the dark a little bit, so we'll see how it 
goes. SQL Alchemy keeps surprising me with features, it's very cool.


Cheers,
 James.



Assuming you don't need that (class level behavior), you don't really need 
@hybrid_property either.   You can just use Python's standard @property.

If you *did* want that, it would be a little tricky, probably would need a 
custom comparator.


   


--
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.



Re: [sqlalchemy] hybrid, relationships and inheritance.

2011-07-08 Thread Michael Bayer

On Jul 7, 2011, at 3:29 PM, James Studdart wrote:

 Hi all,
 I've got a question regarding hybrid properties and how to use them with 
 single table inheritance.
 
 I've got a class hierarchy like this (in semi-pseudo code):
 
 class MyBase(object):
# This has the tablename declared attr, id as primary key, generic table 
 args etc.
 
 class Person(MyBase, Base):
children = relationship('Children')
 
 class SpecialPerson(Person):
partner = relationship('Person')
 
 Okay, so what I want is for SpecialPerson to return both it's own plus it's 
 partners children. But, if I add to list of children of a special person, it 
 only adds to it's local children list. Does that make sense?
 
 This is what I've got now, I'm stabbing around in the dark a little bit, so 
 I'm hoping for some guidance in the correct  way to do this with SQL Alchemy.

What you have is fine, though it is only functional at the instance level, that 
is:

p = Session.query(SpecialPerson).first()
print p.children

At the class level, the __add__() operator doesn't have any behavior for 
relationship() and also cls.partner is again a relationship() which doesn't 
have a _children attribute.   That is, this wouldn't work:

print 
Session.query(SpecialPerson).filter(SpecialPerson.chlidren.contains(some_person))

Assuming you don't need that (class level behavior), you don't really need 
@hybrid_property either.   You can just use Python's standard @property.

If you *did* want that, it would be a little tricky, probably would need a 
custom comparator.


-- 
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.



[sqlalchemy] hybrid, relationships and inheritance.

2011-07-07 Thread James Studdart

Hi all,
 I've got a question regarding hybrid properties and how to use them 
with single table inheritance.


I've got a class hierarchy like this (in semi-pseudo code):

class MyBase(object):
# This has the tablename declared attr, id as primary key, generic 
table args etc.


class Person(MyBase, Base):
children = relationship('Children')

class SpecialPerson(Person):
partner = relationship('Person')

Okay, so what I want is for SpecialPerson to return both it's own plus 
it's partners children. But, if I add to list of children of a special 
person, it only adds to it's local children list. Does that make sense?


This is what I've got now, I'm stabbing around in the dark a little bit, 
so I'm hoping for some guidance in the correct  way to do this with SQL 
Alchemy.


class Person(Mybase, Base):
_children = relationship('Children')

@hybrid_property
def children(self):
return self._children

class SpecialPerson(Person):
partner = relationship('Person')

@hybrid_property
def children(self):
return self._children + self.parter._children

@children.setter
def children(self, value):
self._children = value

Thank you for your time.

Cheers,
 James.




--
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.