我想将“ trans_recipient”字段小部件从下拉菜单更改为文本输入字段。如果是大
我尝试了以下方法:
class SafeTransactionForm(forms.ModelForm):
''' SafeTranSactionForm '''
trans_recipient = forms.CharField(widget=forms.TextInput(attrs={'class': 'special'}))
class Meta:
model = SafeTransaction
fields = [ 'trans_recipient',
'subject',
'arbitrator_name',
'payment_condition',
'amount_to_pay']
产生此结果:
,尽管小部件本身已更改,但实际上尝试使用它会产生如下值错误:
没有我错误地尝试更改小部件的单行代码:
class SafeTransactionForm(forms.ModelForm):
''' SafeTranSactionForm '''
class Meta:
model = SafeTransaction
fields = [ 'trans_recipient',
'subject',
'arbitrator_name',
'payment_condition',
'amount_to_pay']
下拉列表:
我尝试使用语法,尝试更新meta类中的小部件,大多数情况下我都遇到了语法错误,并且在进行了一些Google搜索之后,无法找到这种特定情况的示例。
我们非常欢迎见识和解释。
编辑:
现在我遇到了错误:ValueError: invalid literal for int() with base 10: 'inbox'
UpdateView类:
class SafeTransUpdateView(UpdateView):
'''
This view lets the user Update a SafeTransaction receipt
then send an automatic email to the email address
'''
form_class = SafeTransactionForm
model = SafeTransaction
template_name = "myInbox/safeTrans_update.html"
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(SafeTransUpdateView, self).__init__(*args, **kwargs)
def form_valid(self, form):
trans = form.save(commit=False)
trans.save()
### Send an email to the user upon transaction update.
usr_obj = User.objects.get(customuser=trans.trans_recipient)
user_mail = usr_obj.email
from_email = 'timi.ogunkeye@gmail.com'
contents = "This transaction [ " +trans.payment_condition+" ] has been updated !"
email_subject = 'Transaction Update !'
try:
send_mail(email_subject, contents, from_email, [user_mail], fail_silently=False)
pass
except:
pass
else:
pass
return HttpResponseRedirect('inbox')
我更新的表格:
class SafeTransactionForm(forms.ModelForm):
''' SafeTranSactionForm '''
# trans_recipient = forms.CharField(widget=forms.TextInput(attrs={'class':'special'}))
trans_recipient = forms.ModelChoiceField(queryset=CustomUser.objects.all(),
widget=forms.TextInput(attrs={'value':"username"}), to_field_name="username")
def clean_trans_recipient(self):
data = self.cleaned_data['trans_recipient']
try:
return CustomUser.objects.get(username=data)
except CustomUser.DoesNotExist:
raise forms.ValidationError("No user with this username exists")
class Meta:
model = SafeTransaction
fields = [ 'trans_recipient',
'trans_recipient_email',
'subject',
'arbitrator_name',
'payment_condition',
'amount_to_pay']
safeTrans_update.html:
<h1>TRANSACTION UPDATE: </h1>
<form action="{% url 'ST_update' object.pk %}" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Confirm Update" />
</form>
urls.py:
path('ST_update/<pk>',SafeTransUpdateView.as_view(),name='ST_update'),
我想知道为什么现在会发生此错误:ValueError:int()的无效文字,基数为10:'inbox'。任何信息都非常感谢。
答案 0 :(得分:1)
问题是,Django不知道如何将来自表单字段的数据转换回模型的trans_recipient
属性(应为CustomUser
实例)。
但这不是问题:您可以执行表单清理,然后编写一个自定义函数将字符串转换回CustomUser
对象,例如:
class SafeTransactionForm(forms.ModelForm):
''' SafeTranSactionForm '''
trans_recipient = forms.CharField(widget=forms.TextInput(attrs={'class': 'special'}))
def clean_trans_recipient(self):
data = self.cleaned_data['recipients']
try:
return CustomUser.objects.get(username=data)
except CustomerUser.DoesNotExist:
raise forms.ValidationError("No user with this username exists")
class Meta:
model = SafeTransaction
fields = [ 'trans_recipient', 'subject', 'arbitrator_name',
'payment_condition', 'amount_to_pay' ]
尽管您可能需要对CustomUser
的提取进行一些调整。因此,我们旨在获取此类CustomUser
并将其返回。如果没有这样的CustomUser
,则我们提出一个ValidationError
。