On Tue, Mar 6, 2012 at 1:56 AM, Mike Orr <[email protected]> wrote:
> On Mon, Mar 5, 2012 at 4:13 PM, Daniel Nouri <[email protected]> wrote:
>> On Mon, Mar 5, 2012 at 9:27 PM, Jasper van den Bosch <[email protected]> 
>> wrote:
>>> I have a question for the traversal users:
>>>
>>> do you
>>>
>>> a) load your model from data (i.e. sqla) and add the resource properties
>>> (request, __parent__) in the parent?
>>
>> There's no reason your models can't just implement __parent__ and
>> __name__ as properties themselves.  These have nothing to do with
>> HTTP.  (No need to know about the request for traversal!)
>>
>> An example using SQLAlchemy:
>> https://github.com/Pylons/Kotti/blob/master/kotti/resources.py#L108
>
> So how would you apply this to a normal SQLAlchemy database, that may
> have the concept of name and parent but not in the resource structure?
> In particular, an ordinary 1:many relationship where the parent has
> several children with a backref to itself, but not using the words
> "name" and "parent".
>
> class Main(Base):
>    id = sa.Column(sa.types.Integer, nullable=False, auto_increment=True)
>
>    subs = orm.relationship("Sub", backref="main")
>
> class Sub(Base):
>    id = sa.Column(sa.types.Integer, nullable=False, auto_increment=True)
>    main_id = sa.Column(sa.types.Integer, sa.ForeignKey("Main.id"),
> nullable=False)
>
> In this case, the 'id' fields can serve as the .__name__, converting
> them to a string if necessary. And the 'main' backref is equivalent to
> .__parent__. So would the name and parent simply return these
> properties?

Yes, they would.

> Do we need to do anything to prevent unnecessary queries
> for parent?

After you traversed down to that item, __parent__ will likely already
be in the SQLA identity map cache, so going up back up to the
parent(s) shouldn't cost you any queries.


Dividing your db fields into 'details' and not sounds like a good idea.


-- 
http://danielnouri.org

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to