Skip to content

Instantly share code, notes, and snippets.

@mharju
Created September 1, 2010 20:38
Show Gist options
  • Save mharju/561322 to your computer and use it in GitHub Desktop.
Save mharju/561322 to your computer and use it in GitHub Desktop.
elif isinstance(field, models.ForeignKey) and\
field.rel.limit_choices_to:
key = field.rel.get_related_field().name
choices = field.rel.limit_choices_to
info.choices = list()
for choice in field.rel.to._default_manager.filter(**choices).values():
info.choices.append( (choice[key], unicode(choice.values()[0]),) )
info.shortName = '%s_id' % info.shortName
info.fullName = '%s_id' % info.fullName
setattr(clazz, 'get_%s_display' % info.shortName, lambda x: getattr(x, field.name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment