sql - ORM Django LEFT JOIN + GROUP BY -
i have following models in django project:
class player(models.model): name = models.charfield() class team(models.model): name = models.charfield() class membership(models.model): player = models.foreignkey(player) team = models.foreignkey(team) date_joined = models.datefield() date_leaved = models.datefield(blank=true, null=true)
and want create view on django level perform that:
create view last_contract_expire select player.name, max(membership.date_leaved) player left outer join membership on membership.player.id=player.id group player.id;
django not support sql views (see ticket). django not support sql group by
directly. anyway, achieve this:
def last_contract_expire(): return player.objects.all().annotate(max_date_leaved=max('membership__date_leaved'))
this return players , each player have attributes name
, max_date_leaved
. hope helps
Comments
Post a Comment