On Dec 7, 2011, at 2:07 PM, Julian Berman wrote:

> 
> So, when searching, I see some results that seem to ask this question,
> though none of the solutions are for recent versions of sqlalchemy, so,
> I may as well just ask :): how could I have Location.events be
> order_by'd via Event.date?

Well your EventOccurence has "event_name" already there as the primary key 
so....order_by=event_name:

    location = relationship(Location,
       backref=backref(
           "event_occurences",
           cascade="all, delete-orphan",
           order_by=event_name
       ),
    )

If you needed to join out to Event in order to sort, things would be trickier, 
but that's not the case here (deleted a crapload of text here because I didn't 
notice at first).

Also association_proxy doesn't work usefully with "dynamic" loaders right now.  
 Tried it, it will let you iterate over the collection but you can't use 
order_by(), filter(), or anything like that which is the whole point of using 
"dynamic".



> 
> Secondly, I haven't been able to find an answer for this, but, I see
> that you can configure the association_proxy to use a dict-like
> structure, but it's always keyed by some other unique column on the
> association object.
> 
> Example:
> http://www.sqlalchemy.org/docs/orm/extensions/associationproxy.html#simplifying-association-objects
> 
> But how could I use the actual other *model* as the key and another
> column on the association object as the value. To make it clearer, I'd
> like:

>>>> some_location.events
> {<Event: Gala Ball> : True, <Event: Other Thing> : False}
> 
>>>> some_location["New Event"] = False
>>>> some_location.events
> {..., <Event: New Event> : False}

you can key on any attribute you'd like as illustrated in 
http://www.sqlalchemy.org/docs/orm/collections.html#dictionary-collections.    
you'd again need to lose the "dynamic" thing as that doesn't support 
dictionaries, then set "event" as the attribute in the 
attribute_mapped_collection().  For the True/False you'd need the "events" 
association proxy probably to refer to "successful".

you of course lose the ordering here as dictionaries aren't ordered.

class Location(Base):
    __tablename__ = 'location'
    address = Column(Unicode, primary_key=True)

    events = association_proxy("event_occurences", "successful")


class EventOccurence(Base):
    location = relationship(Location,
       backref=backref(
           "event_occurences",
           cascade="all, delete-orphan",
           collection_class=attribute_mapped_collection("event"),
       ),
    )




> 
> Where the values are the `successful` column on the association object,
> and the (hypothetical) creator used the event objects as the keys, not
> as the example on that page uses them as the values.
> 
> 
> Thanks, sorry for being wordy :),
> Julian
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" 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/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" 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/sqlalchemy?hl=en.

Reply via email to