I have the following ModelForm and I'm getting the results I want, but I can't figure out how to remove the key from the results.
Form:
class TicketActionsForm(ModelForm):
assign = forms.ModelChoiceField(queryset=Users.objects.values('user').filter(account=3), empty_label=" Select Admin ")
View return statement:
return render_to_response('tickets/view.html',
{'ticket':ticket,'ticketactions':TicketActionsForm()},
context_instance=RequestContext(request)
)
Template:
{{ ticketactions.assign }}
Results: (screenshot: http://snapplr.com/z7t7 )
<option value="" selected="selected"> Select Admin </option>
<option value="{'user': u'Chris'}">{'user': u'Chris'} </option>
<option value="{'user': u'mmiller'}">{'user': u'mmiller'}</option>
<option value="{'user': u'millerm'}">{'user': u'millerm'}</option>
<option value="{'user': u'Cindy222'}">{'user': u'Cindy222'}</option>
Edit:
I am able to update the label easily with the following over-ride, but I'm still stumbled on how to change the option values. I can probably edit the data after it is POST'd, but I'd rather clean it up prior to that.
class UserModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj['user']
Resolution:
I ended up doing a little hack in the init to get the results I wanted.
def __init__(self, *args, **kwargs):
super(TicketActionsForm, self).__init__(*args, **kwargs)
newChoices = []
for item in self.fields['assign'].queryset:
choice=(item['user'],item['user'])
newChoices.append(choice)
self.fields['assign'].choices = newChoices