Using your suggestion returns no values: In [9]: Subcategory.objects.values('name').exclude(business=None) Out[9]: []
And there are subcategories: In [10]: Subcategory.objects.values('name') Out[10]: [{'name': u'Agricultural'}, {'name': u'Auctioneers & Estate Agents'}, {'name': u'Construction & Maintenance'}, {'name': u'Dining & Bars'}, {'name': u'Education & Childcare'}, {'name': u'Entertainment'}, {'name': u'Financial & Business Services'}, {'name': u'Financial Banking '}, {'name': u'Health & Beauty'}, {'name': u'Home & Garden'}, {'name': u'Hospitality'}, {'name': u'Legal'}, {'name': u'Leisure'}, {'name': u'Manufacturing'}, {'name': u'Miscellaneous'}, {'name': u'Motoring & Repairs'}, {'name': u'Services'}, {'name': u'Shopping/Retail '}, {'name': u'Transport & Logistics'}, {'name': u'Education '}, {'name': u'Transport'}, {'name': u'Horse Riding'}, {'name': u'Walking'}] And there are business objects with subcategories (naming here is confusing!) Business.objects.values('cat__name') Out[7]: [{'cat__name': u'Agricultural'}, {'cat__name': u'Agricultural'}, {'cat__name': u'Agricultural'}, {'cat__name': u'Agricultural'}, {'cat__name': u'Agricultural'}, {'cat__name': u'Agricultural'}, {'cat__name': u'Legal'}, {'cat__name': u'Shopping/ Retail '}, {'cat__name': u'Financial Banking '}, {'cat__name': u'Shopping/Retail '}, {'cat__name': u'Agricultural'}] Also tried: Subcategory.objects.values('name').exclude(directory=None) FieldError: Cannot resolve keyword 'directory' into field. Choices are: business, category, community, id, name, tourism The problem could be that I have an additional level in that Business is subcalssed to Directory like this: class Category(models.Model): name = models.CharField(max_length=12, unique=True) description = models.TextField() class Subcategory(models.Model): category = models.ForeignKey(Category) name = models.CharField(max_length=30, unique=True) class Directory(models.Model): name = models.CharField(max_length=60) phone = models.CharField(max_length=15) ... class Business(Directory): cat = models.ForeignKey(Subcategory, limit_choices_to = {'category__exact': 2}) Any help much appreciated. This is my second website in Django and starting to become slightly competant, with much support from this users group. Thanks. Phoebe. On Jan 30, 2:23 am, Malcolm Tredinnick <malc...@pointy-stick.com> wrote: > On Thu, 2009-01-29 at 06:44 -0800, phoebebright 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? > > Even if you could do what you wanted here, it wouldn't solve your > problem. You're implicitly using theselectfields and the inner join to > enforce the "a name exists" constraint. Django won't just add an > arbitrary table in via an extra inner join if it's not required. > > There's a better solution to your problem, though. It's a little hard to > tell what the exact queryset will look like, since I don't understand > your models entirely, but this should be close. Firstly, you want > subcategory names, so that's the model to filter on. the constraint > "must have at least one business" is a filter on the business related > attribute being not-None: > > SubCategory.objects.values('name').exclude(business=None) > > Regards, > Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---