Python - Документация

Логика

(VotingAchievement.uuid == nomination_id) if nomination_id else True ~ VotingAchievement.uuid

SQLAlchemy

nominations = await self._session.execute( select(VotingAchievement).where( ~VotingAchievement.is_deleted, VotingAchievement.is_published, (VotingAchievement.uuid == nomination_id) if nomination_id else True, ).order_by(VotingAchievement.position).options( selectinload(VotingAchievement.icon), selectinload(VotingAchievement.preview) ) ) for nomination in nominations.scalars().all(): ...
result = [] for nomination in nominations.scalars().all(): query = select( ExponentVotesAggregation.uuid, ExponentVotesAggregation.title_ru, ExponentVotesAggregation.title_en, func.sum(ExponentVotesAggregation.count).label("count") ).where( ExponentVotesAggregation.created_at >= since, ExponentVotesAggregation.created_at <= till, ExponentVotesAggregation.nomination_id == nomination.uuid, ).group_by( ExponentVotesAggregation.uuid, ExponentVotesAggregation.title_ru, ExponentVotesAggregation.title_en, ).order_by(desc(text("count"))) exponents = await self._session.execute( query ) result.append((nomination, exponents.all(),))

Ссылки