Skip to content

Instantly share code, notes, and snippets.

@hanleybrand
Last active August 29, 2015 14:17
Show Gist options
  • Save hanleybrand/fcd2ede7b5a473448b87 to your computer and use it in GitHub Desktop.
Save hanleybrand/fcd2ede7b5a473448b87 to your computer and use it in GitHub Desktop.
A simple function that will do a fake distinct on a django model; Why? Well, filter.distinct() apparently doesn't work with mysql because #$%#@$%#
def fake_distinct_for_model_field_values(dj_model, field_name):
"""
Perform a fake distinct query on the value_list of a model's objects
example: from myapp.models import ActivityLog
import fake_distinct_for_model_field_values as fake_dist
fake_dist(ActivityLog, 'event_types')
>>> [u'login', u'media-download', u'media-thumbnail', u'media-download-image']
"""
output = set()
for x in dj_model.objects.values_list(field_name):
output.add(x.__getitem__(0))
return list(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment