I may be misunderstanding your question, but I think I have a similar situation. In the dropdown you want the object to be labelled with the name of the location
. If there is a parent
for that location, you would like to show the name of that parent as well within parentheses following the location name.
You can accomplish this by overriding the __init__
method of your ModelForm:
def __init__(self, *args, **kwargs):
def new_label_from_instance(self, obj):
try:
#see if there is a parent or not
p = obj.parent
except:
#no parent; use the location's default label
rep = obj.description
return rep
#if we find a location's parent, p=parent label and d=location label
d = obj.description
rep = "%s (%s)" % (d, p)
return rep
super(PublisherCreateTSCForm, self).__init__(*args, **kwargs)
funcType = type(self.fields['location'].label_from_instance)
self.fields['location'].label_from_instance = funcType(new_label_from_instance, self.fields['location'], forms.models.ChoiceField)
You should know the consequences of doing this before you do it. Check out this old question:
Django CheckboxSelectMultiple override 'choices' from ModelForm
And the docs related to label_from_instance
here (bottom of the linked section): https://docs.djangoproject.com/en/1.7/ref/forms/fields/#modelchoicefield