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? Do we need to do anything to prevent unnecessary queries
for parent?

For large tables, I generally defer the fields that are only used on
the details page and searching -- i.e., not on index pages -- in a
deferral group called 'details'. So if you fetched a Sub and referred
to its parent (probably wanting only the ID and title fields for a
hyperlink, and perhaps a date for sorting), only the fields used
'everywhere' would be loaded.  I guess that would "just work" in a
Resource scenario.

Of course, when fetching a sub, there's no place in the API to specify
whether you'll be wanting all its detail fields, unless you query the
database directly, in which case you can't go through the resource
tree.

-- 
Mike Orr <[email protected]>

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