That's an interesting post but I havn't managed to get it to solve my problem because I'm looking for a list of all categories and that method using FOO_set only seems to work on a single object (get).
Also tried the raw sql method but I get an SQL error (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''business' td, town_subcategory sc WHERE td.cat_id = sc.id' at line 1") Even though if I run the sql manually it is fine. from django.db import connection cursor = connection.cursor() cursor.execute("SELECT DISTINCT sc.name, sc.id FROM town_%s td, town_subcategory sc WHERE td.cat_id = sc.id", [for_cat]) subcats = cursor.fetchall() Any further thoughts most welcome! On Jan 29, 3:48 pm, Almost George <almostgeo...@almostexciting.com> wrote: > On Jan 29, 8:44 am, phoebebright <phoebebright...@gmail.com> wrote: > > > > > I want a distinct list of all the 'cat__names' that exist in > > Subcategory and have at least one entry in Business (Business is a > > subclass of model Directory which might be the problem) > > > dir_query = Business.objects.all().select_related().distinct() > > ... > > subcats = dir_query.values('cat__name','cat').select_related().distinct > > () > > > But the SQL this generates has an additional field in theSELECT > > statement which means the DISTINCT does not have the effect I want and > > selects all entries. > > >SELECTDISTINCT `town_subcategory`.`name`, `town_business`.`cat_id`, > > `town_directory`.`name` > > FROM `town_business` > > INNER JOIN `town_subcategory` ON (`town_business`.`cat_id` = > > `town_subcategory`.`id`) > > INNER JOIN `town_directory` ON (`town_business`.`directory_ptr_id` = > > `town_directory`.`id`) > > ORDER BY `town_directory`.`name` ASC > > > Is there some way of forcing the fields in theselectstatement > > without having to write the whole SQL in raw format? Or another way > > of writing the query? > > Have you tried any of the methods listed in this > article?http://thisweekindjango.com/articles/2007/dec/21/retrieving-selective... > ( Retrieving Selective Fields with Django ) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---