On Nov 10, 12:19 pm, Eric Ongerth <ericonge...@gmail.com> wrote:
>
> Rather than create a specific backref for each subtype of deal, why
> not just continue with your basic 'deals' backref, then attach regular
> python properties to your Merchant class which return just the desired
> "sub-deals".
>
> Something like:
>
> class Merchant(object):
> ...
> @property
> def available_deals(self):
>     return [deal for deal in self.deals if deal.available]
>
> @property
> def expired_deals(self):
>     return [deal for deal in self.deals if deal.expired]
>
> ... and so on.
>

That's an interesting idea.

If I changed the list comprehensions to generators "( deal for deal in
self.deals if deal...)" then I might have a winner.

>
> You could also reverse your order of definition, define Deal first
> with no reference to Merchant, then define Merchant second, with
> mapper properties for each of your type of deal (probably mapped to
> select statements).  But I don't know if it would work to have each of
> those different mapper properties all use 'merchant' (with, of course,
> the uselist=False option to make it 1:1) as the backref identifier.
>

Yeah, I was thinking something similar. It'd require a lot of work to
reorder everything, though.

> On Nov 10, 11:19 am, Jonathan Gardner <jgard...@jonathangardner.net>
> wrote:
>
> > I have two tables, merchants and deals. The merchants table is
> > represented by Merchant and deals table by Deal.
>
> > Each merchant can have 0, 1, or many deals. Some of those deals will
> > be available, while others will be expired or coming soon or deleted.
> > Each deal belongs to exactly one merchant.
>
> > I'd like to setup Merchant to have attributes "deals",
> > "available_deals", "expired_deals", "upcoming_deals", and
> > "deleted_deals".  These would return, obviously, deals from those
> > groups.
>
> > The twist is that I've spread out my tables and ORM classes across
> > several files. I've tried to keep it so that I don't have circular
> > dependencies. That means I've defined Merchant first, and then Deal
> > later, in separate files
>
> > It looks like this:
>
> > in model/merchant.py:
> >   merchants = Table(...)
> >   class Merchant(object): ...
> >   mapper(Merchant, merchants)
>
> > in model/deal.py:
> >   deals = Table(...)
> >   class Deal(object): ...
> >   mapper(Deal, deals, properties=dict(
> >       merchant=relationship(Merchant, backref='deals'),
> >   ))
>
> > What can I sprinkle in model/deal.py's mapper call to add backrefs to
> > 'available_deals', 'deleted_deals', etc...?
>
> > Or am I going about this all wrong?
>
> > Thanks in advance. BTW, SQLAlchemy is, by far, the most superior ORM
> > in the history of the world, bar none, IMHO.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@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.

Reply via email to