coleifer/peewee — pull request #484 by arrowgamer (2014-12-20 21:21:35+00:00)
Title: Support for Expression-based joins when using aggregate_rows()

Joining on an Expression when using aggregate_rows() would cause an error because rel_for_model only handles Fields.

Consider the following data models:

```python
class Organization(BaseModel):
name = CharField()

class Review(BaseModel):
rating = IntegerField()
organization = ForeignKeyField(Organization, related_name='reviews')
```

I want to query for an organization by ID and eagerly load its reviews that are higher than 3.

```python
(Organization
.select(Organization, Review)
.join(Review, JOIN_LEFT_OUTER,
on=((Review.organization == Organization.id) & (Review.rating > 3)).alias('organization'))
.where(Organization.id == 1)
.aggregate_rows())[0]
```

Note the usage of [0] because of #483. The above would fail because rel_for_model does not support Expressions. I am doing the filter on the join instead of the where, because the query should still pull organization data even if there are no reviews above the rating of 3.