Re: A tricky query in one to many relationship - atleast for me:)

2016-08-31 Thread Web Architect
Hi Michal, Thanks for the solution. I tried the solution in a different way: A.objects.annotate( max_date_created=Max('b__date_created')).filter(b__date_created=F('max_date_created'), b__text='ABCD') Hope the above amounts to the same thing as you have suggested. Thanks. On Wednesday,

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-31 Thread Michal Petrucha
On Tue, Aug 30, 2016 at 11:46:14PM -0700, Web Architect wrote: > Hi Erik, > > I tried your solution but there are some issues: > > .filter(date_created=Max('a__b__date_created')) - this is throwing error > saying not proper use of group function. > > If I remove the above, the result isn't cor

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-30 Thread Web Architect
Hi Erik, I tried your solution but there are some issues: .filter(date_created=Max('a__b__date_created')) - this is throwing error saying not proper use of group function. If I remove the above, the result isn't correct where when I go through each 'a' in the result, associated latest B.text

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-30 Thread Web Architect
Thanks Erik. Will try out the solution you mentioned. On Tuesday, August 30, 2016 at 3:59:20 PM UTC+5:30, Erik Cederstrand wrote: > > > > Den 30. aug. 2016 kl. 11.20 skrev Erik Cederstrand < > erik+...@cederstrand.dk >: > > > > I'm not even sure that's possible to express in SQL, but it would >

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-30 Thread Marco Silva
Maybe this can help you https://docs.djangoproject.com/en/1.10/topics/db/aggregation/ terça-feira, 30 de Agosto de 2016 às 07:33:14 UTC+1, Web Architect escreveu: > > Hi, > > I am looking for an elegant and efficient mechanism to have a query filter > or a solution for the following one to many

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-30 Thread Erik Cederstrand
> Den 30. aug. 2016 kl. 11.20 skrev Erik Cederstrand > : > > I'm not even sure that's possible to express in SQL, but it would probably be > quite convoluted if it is. Here's an easier-to-understand solution: > > res = set() > for b in B.objects.all().select_related('a').annotate(Max('date_cre

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-30 Thread Web Architect
Hi Erik, Thanks for the solution. So, I understand we get all instances of B which are max (date created) or latest (date created) for each instance of A and then we check b.text == 'ABCD' and select the corresponding instance of A. Can't we add additional filter to check if text='ABCD' inste

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-30 Thread Erik Cederstrand
> for b in B.objects.all().select_related('a').annotate(Max('date_created')): That should probably be: Max('a__b__date_created') instead. Erik -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving em

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-30 Thread Erik Cederstrand
I'm not even sure that's possible to express in SQL, but it would probably be quite convoluted if it is. Here's an easier-to-understand solution: res = set() for b in B.objects.all().select_related('a').annotate(Max('date_created')): if b.date_created != b.date_created__max: continue

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-30 Thread Web Architect
Hi Mike, Thanks for your response. I am aware of chaining filters. The latest() API in django queryset returns an object and not a queryset. Hence, I cannot add a filter to it. I had thought about using chaining filters but couldn't find a way. On Tuesday, August 30, 2016 at 12:18:56 PM UTC

Re: A tricky query in one to many relationship - atleast for me:)

2016-08-29 Thread Mike Dewhirst
On 30/08/2016 4:33 PM, Web Architect wrote: Hi, I am looking for an elegant and efficient mechanism to have a query filter or a solution for the following one to many relationship model. Please note the following is just an illustration of the models - hope it should provide what I am looking

A tricky query in one to many relationship - atleast for me:)

2016-08-29 Thread Web Architect
Hi, I am looking for an elegant and efficient mechanism to have a query filter or a solution for the following one to many relationship model. Please note the following is just an illustration of the models - hope it should provide what I am looking for: class A(models.Model): name = models.C