Last active
August 29, 2015 14:17
-
-
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 #$%#@$%#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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