last day (16 days later) » 

9:52 PM
0
A: Django making a list of a field grouping by another field in model

TodorUsing a custom Concat aggregate taken from here (an article about the topic) Define this: class Concat(models.Aggregate): def add_to_query(self, query, alias, col, source, is_summary): aggregate = SQLConcat(col, source=models.CharField(), is_summary=is_summary, **self.extra) ...

 
Concat is MySQL group_concat, it will concat the values into string. I've just tested it. There was just a little bug where django tries to cast that string to int, the fix was to send source=models.CharField() in the add_to_query method. Full code provided.
 
is there a similar one for postgresql?
 
Check updated answer. But the solution is untested.
 
i have postgresql 9.3 and i am getting this error: KeyError: u'separator'
ok now i got a new programming error:..... ProgrammingError: syntax error at or near ")" LINE 1: ...tring_agg("irmsapp_mymodel"."value", ",")) AS "values" ...
LINE 1: ...e", string_agg("irmsapp_mymodel"."value" SEPARATOR ...
 
SEPARATOR is for MySQL version of group_concat, the template for Postgre should be '%(function)s(%(field)s, "%(separator)s")'. BTW just for the test you can try a hardcoded version like - return "string_agg(%(field)s, ',')"
 
10:08 PM
hi...i tried the same thing you mentioned..somehow i am still getting the same error
 
can you paste me how you create the queryset, and the error again.
 
y = MyModel.objects.values('item').annotate(values=Concat('value',separator=','))
i tried the hardcoded one and i got this
ProgrammingError: function string_agg(integer, unknown) does not exist
LINE 1: SELECT "irmsapp_product"."model_name", string_agg("irmsapp_s...
 
can you try to execute the query outside django. it should look like this:
select item, string_agg(value, ',')
from mymodel
group by item;
actually i found the problem, pivotaltracker.com/n/projects/228645/stories/13791511, its the integer type of value column, seems like string_agg needs an string column.
 
10:28 PM
ERROR: function string_agg(integer, unknown) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 8
ahh there u got the error too. So is there a fix in the model for this?
 
how to u cast to string in postre
select item, string_agg(value::text, ',')
from mymodel
group by item;
is this working?
 
yeah it is
can you update your answer :)
 
u can add this hardcoded ::text cast into the template like this:
'%(function)s(%(field)s::text, "%(separator)s")'
can you try it
 
somehow it does not work the same way in python
ProgrammingError: column "," does not exist
the query is not being read the same way by python
 
can you see what query django produce?
can you also try the 8.4 solution where we use array_to_string(array_agg(
 
10:39 PM
ah got it finally!!
u ll be amazed to see what i changed
 
:D
 
i changed the single quotes to double and the double quotes to single :P
 
ah
 
it was reading "," so it gets confused with a column
 
cool, but just for the test, try the 8.4 solution :D im curious if it's working
 
10:41 PM
sure
 
i'll fix the quoutes ;]
 
also another question though if thats ok....can we limit the aggregation to just 5 entries? is there something we could do in the command?
 
hmm this is the greatest-n-per-group problem
you should search for in for postre
 
ah ok
 
for it*
in django
it can be solved with 2 queries
18
A: Django Query That Get Most Recent Objects From Different Categories

Tomasz ZielinskiAs far as I know, there is no one-step way of doing this in Django ORM. But you can split it in two queries: bakeries = Bakery.objects.annotate(hottest_cake_baked_at=Max('cake__baked_at')) hottest_cakes = Cake.objects.filter(baked_at__in=[b.hottest_cake_baked_at for b in bakeries]) If id's o...

check this
and maybe this (questions tagged with [greatest-n-per-group] and [postgresql]
http://stackoverflow.com/questions/tagged/greatest-n-per-group+postgresql
 
10:49 PM
cool man!! i ll try it out....thank you so much :)
 
:) you welcome, buttt what happen with my 8.4 test D:
:D
 
oh yeah that works too :)
 
super thank you too ;)
 

  last day (16 days later) »