Hi, I had a problem with using distinct() which was not so distinct. It was because I did not specify an order_by() and the underlying ordering column on the model was in values().
It also selects my ordering column (SQL needs it for the ordering) and the SQL DISTINCT is applied at row level. It might be unfortunate that Django does not implement distinct(column/s). Note that Postgres has this constraint: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions. It would actually be nice if: I could select values('town_name', 'province__country__name', 'province__name').distinct('town_name'). Then the distinct('town_name') should generate a SQL DISTINCT ON (town_name) town_name. The generated SQL should also drop the model ordering if the values(column/s) and ordering column/s do not match (because of the SQL constraint). I am not sure if the other databases supports this. Here is my model & query (we use Postal Code not Zip Code): class Postal_Code(AbstractCommonAll): suburb = models.CharField() box_code = models.CharField() street_code = models.CharField() town_name = models.CharField() province = models.ForeignKey(Province) class Meta: ordering = ("suburb",) For brevity I have left out models: Province (which has ForeignKey to Country). And my statement was: self.filter(town_name__istartswith='Randfo').values('town_name').distinct() - returns multiple rows 'cos it selects suburb for the ordering. self.filter(town_name__istartswith='Randfo').values('town_name').distinct().order_by('town_name') - works 'cos it's the correct way if you use ordering on another column. self.filter(town_name__istartswith='Randfo').values('town_name').distinct().order_by() - this works as I would have expected 'cos it drops the SQL ORDER BY. So it would be nice to do this: self.filter(town_name__istartswith='Randfo').values('town_name', 'province__country__name', 'province__name').distinct('town_name') Regards Chris Matthews -- 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.