Skip to content

Instantly share code, notes, and snippets.

@benatkin
Created January 14, 2010 12:53
Show Gist options
  • Save benatkin/277136 to your computer and use it in GitHub Desktop.
Save benatkin/277136 to your computer and use it in GitHub Desktop.
# 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