Skip to content

Instantly share code, notes, and snippets.

@ksamuel
Created March 16, 2011 15:27
Show Gist options
  • Save ksamuel/872662 to your computer and use it in GitHub Desktop.
Save ksamuel/872662 to your computer and use it in GitHub Desktop.
# create a generator
# 'pairs' now hold an object that generates ((k, v), (k, v), ) it passed into a for loop
pairs = ((f.slug, f.name) for f in FacilityType.objects.all()))
# tuple(pairs) actually run the generator and turn the result into a tuple
# if the tuple is empty, 'or' replace it with a default value to avoid an empty choice
# of course this is a result you don't want in prod, but it's handy for dev with an empty DB
# if you want to make that production friendly, you must be sure to forbid none as a value
# on the validation form
properties_feature_type_choices = tuple(pairs) or (('none', 'None'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment