Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created August 5, 2016 08:48
Show Gist options
  • Save kezabelle/4795a5ba35b37d1298974a6373cb97cd to your computer and use it in GitHub Desktop.
Save kezabelle/4795a5ba35b37d1298974a6373cb97cd to your computer and use it in GitHub Desktop.
how to change the display value for a ModelChoiceField, for mike_papa in #django IRC
class MyModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return "hello"
# or
class MyForm(Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
if 'myfield' in self.fields:
self.fields['myfield'].label_from_instance = lambda obj: "also hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment