Created
January 14, 2010 12:53
-
-
Save benatkin/277136 to your computer and use it in GitHub Desktop.
This file contains 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
# Returns the different values of a field in a django model. | |
# Because I didn't want to check the docs and needed a way that didn't retrieve lots of records from the database. | |
# It was fun to code and uses a couple of Python tricks. | |
def field_values(cls, field): | |
values = [] | |
exclude_args = {field + '__in': values} | |
while True: | |
try: | |
obj = cls.objects.exclude(**exclude_args)[0] | |
values.append(getattr(obj, field)) | |
except IndexError: | |
return values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment