python - Django Max of related entity -


in sql prompt when try this:

select app_name, max(snap_snapinfo.id) snap_snaptopo inner join snap_snapinfo on snap_snaptopo.snp_id = snap_snapinfo.id app_name in ('invoiceserv','cal_host') , stream = 'live' ,  snp_id <= 135353  group app_name; 

i this:

cal_host    126972 invoiceserv 127240 

in django not able program same requirement

>>> st = snaptopo.objects.filter(app_name__in=['invoiceserv','cal_host'], ...                         snp__stream='live', ...                         snp__id__lte=125353).values('app_name', 'snp__id').\ ...                         annotate(snpid=max('snp__id')) >>> print st.query select "snap_snaptopo"."app_name", "snap_snaptopo"."snp_id", max("snap_snaptopo"."snp_id") "snpid" "snap_snaptopo" left outer join "snap_snapinfo" on ("snap_snaptopo"."snp_id" = "snap_snapinfo"."id") ("snap_snapinfo"."stream" = live  , "snap_snaptopo"."app_name" in (invoiceserv, cal_host) , "snap_snaptopo"."snp_id" <= 125353 ) group "snap_snaptopo"."app_name", "snap_snaptopo"."snp_id", "snap_snaptopo"."app_name", "snap_snaptopo"."snp_id", "snap_snapinfo"."snp_ts", "snap_snapinfo"."snp_number" order "snap_snapinfo"."snp_ts" asc, "snap_snapinfo"."snp_number" desc >>> print st [{'snpid': 72538, 'app_name': u'cal_host', 'snp__id': 72538}, {'snpid': 74723, 'app_name': u'invoiceserv', 'snp__id': 74723}, {'snpid': 89231, 'app_name': u'cal_host', 'snp__id': 89231}, {'snpid': 91960, 'app_name': u'cal_host', 'snp__id': 91960}, {'snpid': 96325, 'app_name': u'invoiceserv', 'snp__id': 96325}, {'snpid': 100656, 'app_name': u'cal_host', 'snp__id': 100656}, {'snpid': 104496, 'app_name': u'invoiceserv', 'snp__id': 104496}, {'snpid': 106982, 'app_name': u'cal_host', 'snp__id': 106982}, {'snpid': 120957, 'app_name': u'cal_host', 'snp__id': 120957}] 

i want have django code emulates earlier mentioned sql statement's behavior. missing?

as of now, doing below:

>>> appname in ('invoiceserv', 'cal_host'): ...     max_snp_id = snaptopo.objects.filter(app_name=appname, snp__stream='live', ...                             snp__id__lte=125353).aggregate(max('snp__id')) ...     print appname, max_snp_id ... invoiceserv {'snp__id__max': 104496} cal_host {'snp__id__max': 120957} 

i looking optimal solution using django apis


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -