python - ModelChoiceField 导致 decimal.invalidoperation,即使模型上的 def __str__ 设置为返回名称
问题描述
我有一个 forms.py 和这个小块:
from mainpage.models import Institution
institution = forms.ModelChoiceField(queryset=Institution.objects.all(), empty_label="(Select One)",
to_field_name="institution_name",
widget=forms.Select(
attrs={
'tabindex':'7','class':'form-control'
}
)
)
一旦我有这段代码,它就会导致decimal.invalidoperation,即使我的机构模型就是这样(在另一个应用程序中)
class Institution(models.Model):
objects = models.Manager()
institution_id = models.CharField(max_length=10)
institution_name = models.CharField(max_length=100)
institution_precentages = models.DecimalField(max_digits = 4, decimal_places=2) #might have to change this
def __str__(self):
return self.institution_name
我什至在视图中进行了测试,试图快速渲染所有这些
p = Institution.objects.first()
print(p)
工作正常,不知道还有什么可以尝试或我错过了什么。
编辑:完整的堆栈跟踪:
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[03/Oct/2019 15:31:22] "GET / HTTP/1.1" 200 44551
[03/Oct/2019 15:31:22] "GET /static/img/apo-img/moon.jpg HTTP/1.1" 304 0
[03/Oct/2019 15:31:22] "GET /static/img/apo-img/b4.jpg HTTP/1.1" 200 492566
[03/Oct/2019 15:31:22] "GET /static/img/site-img/saturn.png HTTP/1.1" 200 223493
[03/Oct/2019 15:31:22] "GET /static/js/uza.bundle.js HTTP/1.1" 200 161266
[03/Oct/2019 15:31:22] "GET /static/img/apo-img/b3.jpg HTTP/1.1" 200 2012230
[03/Oct/2019 15:31:22] "GET /static/img/site-img/sloanmilkyway.png HTTP/1.1" 200 2609664
Internal Server Error: /accounts/signup/
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/shathoma/programming/web/django_apo/accounts/views.py", line 75, in signup
return render(request, 'signup.html', {'form': form, 'profile_form': profile_form})
File "/usr/local/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/usr/local/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 993, in render
return render_value_in_context(output, context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 972, in render_value_in_context
value = str(value)
File "/usr/local/lib/python3.7/site-packages/django/utils/html.py", line 388, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "/usr/local/lib/python3.7/site-packages/django/forms/boundfield.py", line 33, in __str__
return self.as_widget()
File "/usr/local/lib/python3.7/site-packages/django/forms/boundfield.py", line 93, in as_widget
renderer=self.form.renderer,
File "/usr/local/lib/python3.7/site-packages/django/forms/widgets.py", line 241, in render
context = self.get_context(name, value, attrs)
File "/usr/local/lib/python3.7/site-packages/django/forms/widgets.py", line 680, in get_context
context = super().get_context(name, value, attrs)
File "/usr/local/lib/python3.7/site-packages/django/forms/widgets.py", line 641, in get_context
context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
File "/usr/local/lib/python3.7/site-packages/django/forms/widgets.py", line 589, in optgroups
for index, (option_value, option_label) in enumerate(self.choices):
File "/usr/local/lib/python3.7/site-packages/django/forms/models.py", line 1137, in __iter__
for obj in queryset:
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 341, in _iterator
yield from self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 72, in __iter__
for row in compiler.results_iter(results):
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1044, in apply_converters
value = converter(value, expression, connection)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/operations.py", line 285, in converter
return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
[03/Oct/2019 15:31:25] "GET /accounts/signup/ HTTP/1.1" 500 219301
Exception ignored in: <generator object cursor_iter at 0x10e417228>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1515, in cursor_iter
cursor.close()
sqlite3.ProgrammingError: Cannot operate on a closed database.
解决方案
当我在一个写得不好的测试中打破外键约束时,我得到了这个错误(在关闭的数据库上操作)
推荐阅读
- neo4j - 在 Neo4J 中检索某些节点相互关系的 Cypher 查询是什么?
- android - Android Emulator 使用系留 iPhone 时无法上网
- android - 无障碍服务标志RequestTouchExplorationMode
- math - 如果非确定性图灵机在 f(n) 空间中运行,那么为什么它在 2^O(f(n)) 时间中运行?
- python - 尝试使用图形创建对角线图案
- python - 不明白为什么这段时间没有停止
- dynamic - 如何在 SpringRouteBuilder 中路由到 Camelhttpquery 中包含的动态 uri?
- docker - docker compose 总是构建 Dockerfile,因此它不依赖于 db
- c++ - 仅允许 QWidget 大小可被特定值整除
- visual-studio - SFTP 连接不支持的密钥格式