我一直试图从基于模型的选项中创建选择输入。我尝试了几次更改,但错误仍然存在。我希望有人能提供帮助。
forms.py代码。
transaction_type = forms.ChoiceField(choices=TRANSACTION_TYPE_CHOICES, widget=forms.RadioSelect(), required=True)
transaction_purpose = forms.ChoiceField(choices=TRANSACTION_PURPOSE_CHOICES, widget=forms.RadioSelect(), required=True)
transaction_payment_method = forms.ChoiceField(choices=TRANSACTION_PAYMENT_METHOD_CHOICES, widget=forms.RadioSelect(), required=True)
transaction_currency = forms.ChoiceField(choices=TRANSACTION_CURRENCY, widget=forms.RadioSelect(attrs={'checked':'checked'}), required=True)
transaction_description = forms.CharField(widget=forms.Textarea(), required=False)
transaction_after_balance = forms.IntegerField(required=False)
transaction_account = forms.ModelChoiceField(required=False, queryset=Accounts.objects.none(), empty_label="Select Account")
class Meta:
model = Transactions
fields = ('transaction_amount','transaction_type', 'transaction_title',
'transaction_description','transaction_currency','transaction_account',
'transaction_purpose', 'transaction_payment_method','transaction_after_balance')
views.py代码
def get(self, request):
employee_name = Employees.objects.get(user=request.user)
form = TransactionForm()
form.fields['transaction_account'].queryset = Accounts.objects.filter(user=request.user)
print(form.fields['transaction_account'].queryset)
args = {}
args['transaction_form'] = form
args['employee_search'] = employee_name
return render(request,'finance/add_transaction.html', args)
def post(self, request):
transaction_form = TransactionForm(request.POST)
transaction_balance = Transactions.objects.filter(user=request.user).latest('transaction_created_at').transaction_after_balance
print (transaction_form)
if transaction_form.is_valid():
form_obj = transaction_form.save(commit=False)
if form_obj.transaction_type == "Credit":
transaction_balance = transaction_balance - form_obj.transaction_amount
form_obj.transaction_after_balance = transaction_balance
else:
print (transaction_balance, form_obj.transaction_amount)
transaction_balance = transaction_balance + form_obj.transaction_amount
form_obj.transaction_after_balance = transaction_balance
form_obj.user= request.user
form_obj.save()
return render(request,'dashboard')
return render(request,'Error')
html模板代码
<div class="col-lg-3">
<label>
Account:
</label>
<div class="">
{% render_field transaction_form.transaction_account class="form-control m-select2" id="account_select" name="transaction_account" %}
</div>
</div>
模板呈现正常。 This is the rendered template showing options.
呈现的HTML代码:
<select name="transaction_account" id="account_select" class="form-control m-select2 select2-hidden-accessible" data-select2-id="account_select" tabindex="-1" aria-hidden="true">
<option value="" selected="" data-select2-id="2">Select Account</option>
<option value="1" data-select2-id="13">MCB</option>
</select>
当我提交表格时。该表格无效。如果删除验证并尝试直接保存表单对象。它引发一个例外,即“ 1”应为Account的实例。
请提供帮助,因为我无法弄清楚我在做什么错。
models.py代码
class Transactions(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
transaction_created_at = models.DateTimeField(auto_now_add='True')
transaction_amount = models.IntegerField()
transaction_title = models.CharField(max_length=150, blank=True, null=True)
transaction_type= models.CharField(max_length=10)
transaction_description= models.CharField(max_length=1000)
transaction_after_balance= models.IntegerField(blank=True, null=True)
transaction_currency=models.CharField(max_length=20)
transaction_account=models.ForeignKey(Accounts, on_delete=models.CASCADE, blank=True, null=True)
transaction_purpose=models.CharField(max_length=300)
transaction_payment_method = models.CharField(max_length=20)
我已经为models.py文件添加了代码。
更新
我已经根据views.py中select的id手动分配了对象。其他ModelSelectFields工作正常。