I have a problem filtering out duplicates from a query.
I’m using Django 1.4 with Postgres 8.4.13
I use this query on my model object (it is a jquery autocomplete)
term = request.GET.get('term')
field = request.GET.get('field')
filter = field + '__' + 'icontains'
model_results = teilnehmer.objects.filter(**{filter: term}).order_by().distinct()
results = [ getattr(x, field) for x in model_results ]
output = simplejson.dumps(results)
It works nice, giving me results for any search term in any field of the model.
But it shows every single hit. So if the name “Smith” is in the table 10 times, it gives me an array of 10 times “Smith”.
It should work as a suggestion-tool. I read about distinct and tried to use it (as above) but it doesn’t work.
Any ideas?
Thanks in advance.
Conrad
Pass field name to
distinctfunction:Also your approach is very insecure because you should validate
GETvalues before passing them toQuerySet. Use django forms maybe?Also read the docs about
distinctfunction here