2011-03-25 89 views
0

我遇到了clean_data似乎工作的問題。我想在會話中存儲cleared_data,以便稍後重新填充表單對象。問題是我的選擇字段似乎存儲下拉列表的顯示名稱而不是實際值。例如:Python - Django - Form choicefield和cleaned_data

<select name="dropdown_element"> 
<option value="1">Red</option> 
<option value="2">Blue</option> 
</select> 

如果我選擇了紅色和使用form.cleaned_data [「dropdown_element」]我會得到的顯示名稱和紅色1.沒有價值有沒有辦法來獲取值,而不是標籤?

編輯

VIEW:

def vehicle_add_fleet(request): 
     if request.POST: 
      fleet_form = FleetForm(request.POST, prefix='fleet') 
      count_form = CountForm(request.POST, prefix='count', label='# of Odometer Readings') 
      if fleet_form.is_valid() and count_form.is_valid(): 
       request.session['add'] = {} 
       request.session['add']['fleet'] = fleet_form.cleaned_data 
       return HttpResponseRedirect('/vehicle/add/general/%s/' % (count_form.cleaned_data['count'])) 
     else: 
      fleet_form = FleetForm(prefix='fleet') 
      count_form = CountForm(prefix='count', label='# of Odometer Readings') 
     return render_to_response('add/fleet.html', {'fleet_form': fleet_form, 'count_form': count_form}) 

FORM:

class FleetForm(forms.models.ModelForm): 
    home_location = forms.ModelChoiceField(queryset=Location.objects.filter(home_only=True), required=False, label=_('Home Location')) 
    number = forms.IntegerField(max_value=99999, label=_('Fleet Number')) 
    ari_number = forms.IntegerField(max_value=999999, required=False, label=_('ARI Number')) 
    class Meta: 
     model = Fleet 
     exclude = ('status') 

MODEL:

class Fleet(models.Model): 
    #One-to-Many 
     status = models.ForeignKey(Status, verbose_name=_('Status'), default=3) 
     pwgsc_spec_code = models.ForeignKey(PWGSCSpecCode, verbose_name=_('PWGSC Specification Code')) 
     current_location = models.ForeignKey(Location, related_name='fleet_current', blank=True, null=True, verbose_name=_('Current Location')) 
     home_location = models.ForeignKey(Location, related_name='fleet_home', blank=True, null=True, verbose_name=_('Home Location')) 
     #Fields 
     number = models.PositiveIntegerField(verbose_name=_('Fleet Number')) 
     pool_identifier = models.CharField(max_length=1, verbose_name=_('Pool Identifier')) 
     ari_number = models.PositiveIntegerField(blank=True, null=True, verbose_name=_('ARI Number')) 
     credit_card_number = models.CharField(max_length=16, blank=True, verbose_name=_('Credit Card Number')) 
     credit_card_expiry_date = models.DateField(blank=True, null=True, verbose_name=_('Credit Card Expiry Date')) 
     company_name = models.CharField(max_length=16, blank=True, verbose_name=_('Company Name')) 
     in_service_date = models.DateField(blank=True, null=True, verbose_name=_('In Service Date')) 
     reason = models.TextField(blank=True, verbose_name=_('Reason')) 

     class Meta: 
      verbose_name = _('Fleet') 
      verbose_name_plural = _('Fleets') 

所以基本上我想添加ŧ他將車隊的數據(清洗數據)形成會話。然而,當我得到的地方,我要救艦隊形式的頁面,我嘗試填充它:

def vehicle_add(request): 
    #save non-partial forms 
    form = FleetForm(request.session['add']['fleet']) 
    if form.is_valid(): 
     return HttpResponse('valid') 
    else: 
     return HttpResponse('invalid') 

而且我上form.is_valid()的錯誤行:

類型錯誤在/車輛/添加/車隊/ INT() 參數必須是字符串或數字

我當前的理論是,fleet_form.cleaned_data [「home_location」](例如)存儲的顯示值和而不是值fi中的整數場

其他信息:

我使用Django 1.1升級到1.3的Django。在所有其他示例中,我看到人們似乎獲得了下拉值,而不是下拉顯示值。希望這是足夠的信息!

感謝所有幫助

+1

你可以添加你使用的代碼來獲取表單以及表單對象(你的django的forms.Form類)的數據。 – Lin 2011-03-25 18:12:54

+0

對不起沒有意識到他們是一個編輯按鈕。 – 2011-03-25 20:04:37

回答

0

假設你的模式是:

class SomeModel(models.Model): 
    colour = models.IntegerField(choices=[(1, 'Red'), (2, 'Blue'),]) 

您的形式:

class SomeModelForm(forms.ModelForm): 
    class Meta: 
     model = SomeModel 

顯示上述表單模板和GET後/ POST應該給你顏色的整數值。

0

看起來像給表單加上前綴會導致一些奇怪的行爲。我不熟悉使用前綴,所以我不能給你任何建議,但你不是在你的vehicle_add_view前綴。

編輯

我做了一些測試

無前綴

>>> from test.forms import TestForm 
>>> form = TestForm(data={'test_field':'test_data'}) 
>>> form.__dict__ 
{'files': {}, 'is_bound': True, 'error_class': <class 'django.forms.util.ErrorList'>, 
'empty_permitted': False, 'fields': {'test_field': <django.forms.fields.CharField 
object at 0x99d05ac>}, 'initial': {}, 'label_suffix': ':', 'prefix': None, 
'_changed_data': None, 'data': {'test_field': 'test_data'}, '_errors': None, 
'auto_id': 'id_%s'} 
>>> form.is_valid() 
True 
>>> form.cleaned_data 
{'test_field': u'test_data'} 
>>>  

前綴:

>>> form = TestForm(data={'test_field':'test_data'}, prefix="foo") 
>>> form.__dict__ 
{'files': {}, 'is_bound': True, 'error_class': <class 'django.forms.util.ErrorList'>, 
'empty_permitted': False, 'fields': {'test_field': <django.forms.fields.CharField object 
at 0x99d03cc>}, 'initial': {}, 'label_suffix': ':', 'prefix': 'foo', '_changed_data': 
None, 'data': {'test_field': 'test_data'}, '_errors': None, 'auto_id': 'id_%s'} 
>>> form.is_valid() 
True 
>>> form.cleaned_data 
{'test_field': u''} 
>>> 

注意到,即使它看起來像數據在表單中,deleted_data也缺少值。希望能夠幫助您排除故障。