我想验证form.py上不是表单字段的字段是否可能 这是我的模型形式:
class Pay_bills(forms.ModelForm):
class Meta:
model = Recharge_request
fields = ['mobile_no','amount']
def clean_amount(self):
amount = self.cleaned_data.get('amount', None)
print(self.cleaned_data.get('amount'))
if ( amount < 1 ):
raise forms.ValidationError("Please enter valid amount")
return amount
和我的html页面
<div class="form-group" style="display:none;" id="consumer_no_div">
<label for="inputFirstName" id="field-label" >Consumer No:</label>
<div>
{% render_field form.mobile_no class="form-control" placeholder="Customer No"%}
<div class="form-group" >
<label> Bill Amount:</label>
<div>
{% render_field form.amount class="form-control" placeholder="Amount"%}
</div>
</div>
<div class="form-group" >
<label>Service Provider:</label>
<div >
<select required class="form-control" id="sel1" name="service_provider" name="sel">
<option value="">Select Service Provider</option>
{%for provider in ser_provider%}
<option value="{{provider.id}}">{{provider}}</option>
{%endfor%}
</select>
</div>
</div>
这里我要验证服务提供商,这不是我的表单字段..我可以在forms.py??hhanks提前做
答案 0 :(得分:1)
为什么不把它变成表格字段?
class Pay_bills(forms.ModelForm):
service_provider = forms.ChoiceField(choices=ser_providers) # or ModelChoiceField(queryset=ser_providers)
class Meta:
model = Recharge_request
fields = ['mobile_no','amount']
def clean_service_provider(self):
... whatever....