запрос с группировкой

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class CitizensView(RestView):
""" Военнослужащие, Граждане
"""
join = outerjoin(m.Citizens, m.Documents, m.Citizens.id == m.Documents.citizen_id)
query = select([m.Citizens, m.PermanentHousing, m.Documents], group_by=m.Citizens.id).select_from(join).where(
and_(
m.Citizens.id == m.PermanentHousing.citizen_id,
# m.Citizens.status == 1,
or_(
m.Citizens.citizen_id == 0,
m.Citizens.citizen_id == None
)
)
).distinct(m.Citizens.id).apply_labels()