开发者

How can I remove the key from this ModelForm

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;u开发者_开发问答ser&#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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜