Andy Grover wrote:

> Hi, I'm porting my app from SQLObject. Given the model below:
>
> [snip]
>
> I would like to create a property for Person that takes
> a relationship and then further filters it (e.g. in the
> above example, by date.) In SO I'd define the relation
> as a SQLRelatedJoin so that a.events would return a
> QueryObj instead of an actual list, so I could do "return
> self.events.filter(Event.q.date >= date.today())"

Here is a little example of how you could implement this:

     from elixir import *
     from sqlalchemy import and_
     from datetime import datetime

     class Event(Entity):
         name = Field(String(64))
         date = Field(DateTime)
         attendees = ManyToMany("Person")

         def __repr__(self):
             return self.name

     class Person(Entity):
         name = Field(String(64))
         events = ManyToMany("Event")

         @property
         def future_events(self):
             return Event.query.filter(and_(
                 Event.attendees.any(id=self.id),
                 Event.date >= datetime.now().date()
             ))

     setup_all()
     metadata.bind = 'sqlite:///'
     metadata.create_all()

     p1 = Person(name='Jon')
     p2 = Person(name='Andy')

     e1 = Event(name='Event One', date=datetime(2010, 1, 1))
     e2 = Event(name='Event Two', date=datetime(2010, 1, 2))
     e3 = Event(name='Event Three', date=datetime(2007, 12, 12))
     e1.attendees.append(p1)
     e1.attendees.append(p2)
     e2.attendees.append(p1)
     e3.attendees.append(p1)
     e3.attendees.append(p2)

     session.commit()

     p1 = Person.get(1)
     print p1.future_events.all()

     p2 = Person.get(2)
     print p2.future_events.all()

I hope this helps.

--
Jonathan LaCour
http://cleverdevil.org

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

Reply via email to