Skip to content

Instantly share code, notes, and snippets.

@arsatiki
Created April 22, 2009 08:41
Show Gist options
  • Save arsatiki/99665 to your computer and use it in GitHub Desktop.
Save arsatiki/99665 to your computer and use it in GitHub Desktop.
def choices(name, choicemap):
"""{name: value} -> C.name = value """
members = choicemap.copy()
members[name] = tuple((k, k) for k in choicemap.values())
return type('%s_choices' % name, (), members)
# sexes = {'MALE': 'male', 'FMLE': 'female'}
# class FooModel(models.Model, choices('SEXES', sexes))
# => autogenerates FooModel.MALE, FooModel.FMLE and FooModel.SEXES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment