1

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="{&#39;user&#39;: u&#39;Chris&#39;}">{&#39;user&#39;: u&#39;Chris&#39;}    </option> 
<option value="{&#39;user&#39;: u&#39;mmiller&#39;}">{&#39;user&#39;: u&#39;mmiller&#39;}</option> 
<option value="{&#39;user&#39;: u&#39;millerm&#39;}">{&#39;user&#39;: u&#39;millerm&#39;}</option> 
<option value="{&#39;user&#39;: u&#39;Cindy222&#39;}">{&#39;user&#39;: u&#39;Cindy222&#39;}</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
Chris
  • 673
  • 1
  • 10
  • 28

1 Answers1

1

This answer has a simple way of changing the labels used in a ModelChoiceField.

When you use a ModelChoiceField, you probably want to call Users.objects.all() rather than Users.objects.values('user').

What you want to do then, would be to have the following code:

class UserModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.get_full_name()

and when you make your form, you should have the following code:

class TicketActionsForm(ModelForm):
    assign = forms.ModelChoiceField(queryset=Users.objects.all().filter(account=3), empty_label="  Select Admin  ")

This way a set (more accurately, a QuerySet) of rows is passed to the ModelChoiceField object and it will have access to all the fields. Using the code above, you can control the label and when you change objects.values('user').filter to objects.all().filter, the value property of the <option> tag will become the id of the row. You can then use the incoming id from request.POST or request.GET to store it in your database as any other field you like by running a query. However, in most cases, you probably want to store it as id in the database.

Community
  • 1
  • 1
Umang
  • 5,196
  • 2
  • 25
  • 24
  • Alright, so after some playing around I can only get the label to change. I've been searching around for a value override but can't find anything. Any idea's on how I can do that? – Chris Jul 18 '11 at 03:13
  • I've updated my answer. I had not read the code you passed to the queryset argument carefully. – Umang Jul 18 '11 at 12:57
  • hmm, could have sworn i tried that last night.. working now though. Thank you for the help. – Chris Jul 18 '11 at 20:49