// Django 2.2.2 // sqlite3
我正在使用Django Aggregate将各种统计值传递给Django模板。
平均,计数运行良好。但是Variance无法正常工作,并给我OperationError。
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from(
[('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])
val_map = {'A': 1.0,
'D': 0.5714285714285714,
'H': 0.0}
values = [val_map.get(node, 0.25) for node in G.nodes()]
%matplotlib inline
nx.draw_networkx(G, cmap = plt.get_cmap('jet'), node_color = values)
nx.draw(G)
plt.show()
AttributeError Traceback (most recent call last)
<ipython-input-13-127734f4f34b> in <module>()
14 values = [val_map.get(node, 0.25) for node in G.nodes()]
15 get_ipython().magic('matplotlib inline')
---> 16 nx.draw_networkx(G, cmap = plt.get_cmap('jet'), node_color = values)
17
18 nx.draw(G)
1 frames
/usr/local/lib/python3.6/dist-packages/networkx/drawing/nx_pylab.py in draw_networkx_edges(G, pos, edgelist, width, edge_color, style, alpha, edge_cmap, edge_vmin, edge_vmax, ax, arrows, label, **kwds)
520 lw = width
521
--> 522 if not cb.is_string_like(edge_color) \
523 and cb.iterable(edge_color) \
524 and len(edge_color) == len(edge_pos):
AttributeError: module 'matplotlib.cbook' has no attribute 'is_string_like'
当我在模型方法中编写时,方差起作用。 但是在views.py中,无法正常工作。 我不知道这两者之间有什么区别。
views.py
Traceback (most recent call last):
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/miguel/Outsourcings/myProject/project_app/views.py", line 116, in project_report
print(subject)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/models/query.py", line 274, in __iter__
self._fetch_all()
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/debug_toolbar/panels/sql/tracking.py", line 186, in execute
return self._record(self.cursor.execute, sql, params)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/debug_toolbar/panels/sql/tracking.py", line 124, in _record
return method(sql, params)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/miguel/Outsourcings/myProject/myvenv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: user-defined aggregate's 'finalize' method raised error
[03/Jun/2019 20:41:24] "GET /project/report/7 HTTP/1.1" 500 224730
models.py
def project_report(request, pk):
project = get_object_or_404(ProjectModel.objects, pk=pk)
subject = project.subject_for_project.filter(status='on', party_status='join').annotate(feel_avg=Avg('datamodel__feeling'),
pain_avg=Avg('datamodel__pain'),
side_count=Count(Case(
When(datamodel__flag='side', then=1),
output_field=IntegerField(),
)),
# this part is not working
pain_variance=Variance('datamodel__pain')
)
print(subject)
context = {
'project':project,
'subject':subject,
}
return render(request, 'project/project_report.html, context)
根据django 2.2发行说明,我读到sqlite环境也支持variance,但是如果我不能在视图中使用它,我想找到另一种方法。 谢谢。
答案 0 :(得分:0)
尽管使用version 2.2 of Django,但在sqlite数据库环境中它似乎仍不完全支持Variance。
使用类中的方法创建错误时不会引发错误。但是,当我在views.py中创建FBV并从父模型加载FBV时,会发生错误。
我将本地数据库更改为仅Postgresql,所以错误消失了。