[英]Django: models.BooleanField(default=False) always save value as 1(True)
我有一個 Boolean model 字段payment_received
,默認值設置為 False。 從前端下拉列表中,它以0
或1
形式出現。無論下拉列表 I select 中的值是什么,它始終在表中保存為 1(True)。 我正在嘗試對其進行調試,並且我可以驗證正確的值來自 HTML 表單,但是在通過 Django 表單完成清潔后,它始終會產生True
值。
我的 Model
class InvoiceHeader(models.Model):
class Meta:
db_table = 'invoice_hdr'
invoice_no = models.CharField(max_length=50)
client = models.ForeignKey(Client, on_delete=models.DO_NOTHING)
campaign = models.ForeignKey(CampaignHeader, on_delete=models.DO_NOTHING, null=True)
invoice_date = models.DateField()
invoice_amount = models.DecimalField(max_digits=11, decimal_places=2)
cgst = models.DecimalField(max_digits=11, decimal_places=2)
sgst = models.DecimalField(max_digits=11, decimal_places=2)
igst = models.DecimalField(max_digits=11, decimal_places=2)
total_amount = models.DecimalField(max_digits=11, decimal_places=2)
payment_received = models.BooleanField(default=False)
def __str__(self):
return self.invoice_no
我的表格
class InvoiceHeaderForm(forms.ModelForm):
class Meta:
fields = ('invoice_no','invoice_date','campaign','client', 'invoice_amount','cgst','sgst','igst','total_amount','payment_received')
model = InvoiceHeader
invoice_no = forms.CharField(max_length=50)
invoice_date = forms.DateField()
client = forms.ModelChoiceField(queryset=Client.objects.all())
campaign = forms.ModelChoiceField(queryset=CampaignHeader.objects.all())
invoice_amount = forms.DecimalField(max_digits=11, decimal_places=2)
cgst = forms.DecimalField(max_digits=11, decimal_places=2)
igst = forms.DecimalField(max_digits=11, decimal_places=2)
sgst = forms.DecimalField(max_digits=11, decimal_places=2)
total_amount = forms.DecimalField(max_digits=11, decimal_places=2)
payment_received = forms.BooleanField()
def clean_payment_received(self):
val = self.cleaned_data['payment_received']
print('TEST1', val) #<---- always True
return val
def clean_invoice_no(self):
invoice_no = self.cleaned_data.get('invoice_no')
if InvoiceHeader.objects.filter(invoice_no=invoice_no).exists() and not str(self.instance):
raise forms.ValidationError('Invoice no. already exist!')
return invoice_no
模板
<form action="{{ url }}" method="POST" id="invoice_form">
<input type="hidden" name="edit_mode" value="{% if invoice %}{{'1'}}{% else %}{{'0'}}{% endif %}">
{% csrf_token %}
<div class="card shadow py-2">
<div class="card-body">
<div class="row ">
<div class="col-md-6">
<div class="form-group">
<label for="invoice_no">Invoice No.<sup class="text-danger">*</sup></label>
<input type="text" tabindex="1" class="form-control" id="invoice_no"
name="invoice_no" placeholder="Enter campaign name"
value="{% if invoice %}{{invoice.invoice_no}}{% endif %}">
<small class="error-msg form-text text-danger"></small>
</div>
<div class="form-group">
<label for="client">Client<sup class="text-danger">*</sup></label>
<select tabindex="3" class="form-control selectpicker" data-live-search="true"
id="client" name="client">
{% for client in clients %}
<option data-state="{{client.state}}" id="client" value="{{ client.id }}"
{% if invoice and invoice.client_id == client.id %}{{'selected'}}{% endif %}>
{{ client }}</option>
{% endfor %}
</select>
<small class="error-msg form-text text-danger"></small>
</div>
<div class="form-group">
<label for="invoice_amount">Invoice Amount<sup class="text-danger">*</sup></label>
<input type="text" data-type='currency' tabindex="5" class="form-control" id="invoice_amount"
name="invoice_amount" placeholder="Enter invoice amount"
value="{% if invoice %}{{invoice.invoice_amount}}{% endif %}">
<small class="error-msg form-text text-danger"></small>
</div>
<div class="form-group">
<label for="igst">IGST<sup class="text-danger">*</sup></label>
<input type="text" data-type='currency' tabindex="7" class="form-control" id="igst"
name="igst" placeholder="Enter invoice amount"
value="{% if invoice %}{{invoice.igst}}{% endif %}">
<small class="error-msg form-text text-danger"></small>
</div>
<div class="form-group">
<label for="payment_received">Payment Received?<sup class="text-danger">*</sup></label>
<select tabindex="9" class="form-control selectpicker" data-live-search="true"
id="payment_received" name="payment_received">
<option value="0" {% if invoice and not invoice.payment_received %}{{'selected'}}{% endif %}>NO</option>
<option value="1" {% if invoice and invoice.payment_received %}{{'selected'}}{% endif %}>YES</option>
</select>
<small class="error-msg form-text text-danger"></small>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="invoice_date">Invoice Date<sup class="text-danger">*</sup></label>
<input type="text" tabindex="2" class="form-control datepicker" id="invoice_date"
name="invoice_date" placeholder="Enter invoice date"
value="{% if invoice %}{{invoice.invoice_date|date:'d/m/Y'}}{% endif %}">
<small class="error-msg form-text text-danger"></small>
</div>
<div class="form-group">
<label for="campaign">Campaign<sup class="text-danger">*</sup></label>
<select tabindex="4" class="form-control selectpicker" data-live-search="true"
id="campaign" name="campaign">
{% if invoice %}
{% for campaign in campaigns %}
<option value="{{campaign.id}}" {% if campaign == invoice.campaign %}{{'selected'}}{% endif %}>{{campaign}}</option>
{% endfor %}
{% endif %}
</select>
<small class="error-msg form-text text-danger"></small>
</div>
<div class="form-group">
<label for="cgst">CSGT<sup class="text-danger">*</sup></label>
<input type="text" data-type='currency' tabindex="6" class="form-control" id="cgst"
name="cgst" placeholder="Enter invoice amount"
value="{% if invoice %}{{invoice.cgst}}{% endif %}">
<small class="error-msg form-text text-danger"></small>
</div>
<div class="form-group">
<label for="sgst">SGST<sup class="text-danger">*</sup></label>
<input type="text" data-type='currency' tabindex="8" class="form-control" id="sgst"
name="sgst" placeholder="Enter invoice amount"
value="{% if invoice %}{{invoice.sgst}}{% endif %}">
<small class="error-msg form-text text-danger"></small>
</div>
<div class="form-group">
<label for="total_amount">Total Amount<sup class="text-danger">*</sup></label>
<input type="text" data-type='currency' tabindex="10" class="form-control" id="total_amount" readonly
name="total_amount" placeholder="Enter invoice amount"
value="{% if invoice %}{{invoice.total_amount}}{% endif %}">
<small class="error-msg form-text text-danger"></small>
</div>
</div>
</div>
</div>
</div>
<br>
</form>
任何幫助,將不勝感激....
這可能有效(在 forms.py 中)。 您還可以知道您使用的是哪個 django 版本嗎?
[...]
payment_received = forms.BooleanField(widget=CheckboxInput(default=False), required=False)
[...]
html 模板的“選定”值會將布爾字段呈現為 True。 因為通常 html 端的 booleanfield 是一個復選框,如果它被選中則為 True。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.