我的模型中有一个十进制字段
current_CGPA = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=4)
我正在使用Modelform验证我的表单
class StudentAcademicsForm(ModelForm):
class Meta:
model = Student
fields = ['current_CGPA']
我已使用自定义模板进行表单呈现
<label class="form-label" for="current_CGPA">CGPA upto previous semester</label>
<input class="form-control" type="number" step="0.01" placeholder="CGPA" id="current_CGPA" name="current_CGPA" {% if student.current_CGPA is not None %}value="{{student.current_CGPA}}"{% endif %} required min=0 max=10/>
{{form.current_CGPA.errors}}
当我提交请求并验证表单(像这样)时
user = request.user
student = Student.objects.get(user=user)
if request.method == 'POST':
form = StudentAcademicsForm(request.POST, instance=student)
if form.is_valid():
form.save()
else:
form = StudentAcademicsForm()
发生此错误:
TypeError at /student/home
conversion from dict to Decimal is not supported
在通过stacktrace时,我发现字段“ current_CGPA”上的get_attr()
方法以格式返回POST的所有属性
{
'current_CGPA':3,...
}
我认为其预期仅返回值“ 3”
但这是由Django内置代码完成的
那是个错误吗?
有人使用过DecimalField吗?
如果是这样,请纠正我是否做过的事情
Stacktrace如下所示:
Environment:
Request Method: POST
Request URL: http://localhost:8000/student/home
Django Version: 2.0
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'gep_app.apps.GepAppConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/code/gep_project/gep_app/views.py" in student_home
49. if form.is_valid():
File "/usr/local/lib/python3.7/site-packages/django/forms/forms.py" in is_valid
179. return self.is_bound and not self.errors
File "/usr/local/lib/python3.7/site-packages/django/forms/forms.py" in errors
174. self.full_clean()
File "/usr/local/lib/python3.7/site-packages/django/forms/forms.py" in full_clean
378. self._post_clean()
File "/usr/local/lib/python3.7/site-packages/django/forms/models.py" in _post_clean
401. self.instance.full_clean(exclude=exclude, validate_unique=False)
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py" in full_clean
1144. self.clean_fields(exclude=exclude)
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py" in clean_fields
1186. setattr(self, f.attname, f.clean(raw_value, self))
File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py" in clean
606. value = self.to_python(value)
File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py" in to_python
1553. return decimal.Decimal(value)
Exception Type: TypeError at /student/home
Exception Value: conversion from dict to Decimal is not supported`