如何在 django 中渲染时使用 for 语句简化代码 views.py
How to streamline code using for statement when rendering in django's views.py
我正在使用 django,下面的代码运行效率低下。有没有办法通过使用 for 语句来缩短创建和附加列表的方法,如下面的代码所示?列表用于在顶点图表中创建图形 javascript。
[views.py]
annotations = {}
types = ('A', 'B', 'C', 'D', 'E', 'F')
for type in types:
annotations[type] = Count('id', filter=Q(type=type))
annotations[f'r_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Recruiting'))
annotations[f'N_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Not yet recruiting'))
annotations[f'H_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Holding'))
annotations[f'C_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Completed'))
counts = Research.objects.values('teacher').annotate(**annotations).values('teacher', *annotations.keys())
teacher = [];
A = [];
B= [];
C= [];
D= [];
E= [];
F= [];
r_A = [];
r_B = [];
r_C = [];
r_D = [];
r_E = [];
r_F = [];
...
C_E = [];
C_F = [];
for count in counts:
teacher.append(str(count['teacher']))
A.append(str(count['A']))
B.append(str(count['B']))
C.append(str(count['C']))
D.append(str(count['D']))
E.append(str(count['E']))
F.append(str(count['F']))
r_A.append(str(count['r_A']))
r_B.append(str(count['r_B']))
r_C.append(str(count['r_C']))
r_D.append(str(count['r_D']))
r_E.append(str(count['r_E']))
r_F.append(str(count['r_F']))
...
C_E.append(str(count['C_E']))
C_F.append(str(count['C_F']))
return render(request, 'graph.html',
{
'teacher': teacher,
'A': A,
'B': B,
'C': C,
'D': D,
'E': E,
'F': F,
'r_A': r_A,
'r_B': r_B,
'r_C': r_C,
'r_D': r_D,
'r_E': r_E,
'r_F': r_F,
...
'C_E': C_E,
'C_F': C_F
})
[graph.html]
series: [{% if is_recruiting == 'Recruiting' %}
{
name: 'A',
data: {{ r_A | safe }}
}, {
name: 'B',
data: {{ r_B | safe }}
}, {
name: 'C',
data: {{ r_C | safe }}
}, {
name: 'D',
data: {{ r_D | safe }}
}, {
name: 'E',
data: {{ r_E | safe }}
}, {
name: 'F',
data: {{ r_F | safe }}
},{% elif is_recruiting == 'ALL' %}
{
name: 'A',
data: {{ A | safe }}
}, {
name: 'B',
data: {{ B | safe }}
}, {
name: 'C',
data: {{ C | safe }}
}, {
name: 'D',
data: {{ D | safe }}
}, {
name: 'E',
data: {{ E | safe }}
}, {
name: 'F',
data: {{ F | safe }}
}, ... {% elif is_recruiting == 'Completed' %}
{
name: 'A',
data: {{ C_A | safe }}
}, {
name: 'B',
data: {{ C_B | safe }}
}, {
name: 'C',
data: {{ C_C | safe }}
}, {
name: 'D',
data: {{ C_D | safe }}
}, {
name: 'E',
data: {{ C_E | safe }}
}, {
name: 'F',
data: {{ C_F | safe }}
},{% endif %}
],
这是一个使用 apexchart.js 创建图形的 javascript。
这部分也是用if语句继续代码,所以可读性不好。有什么办法可以缩短这部分吗?
类似于:
for count in counts:
teacher.append(str(count['teacher']))
A.append(str(count['A']))
B.append(str(count['B']))
C.append(str(count['C']))
D.append(str(count['D']))
E.append(str(count['E']))
F.append(str(count['F']))
可能是 re-written 使用字典而不是多个列表:
count_dict = defaultdict(list)
for type_, count in itertools.product(types, counts):
count_dict[type_].append(str(count[type_]))
(它是 type_
而不是 type
的原因是因为 type
是一个保留字......只需选择另一个 var 名称,例如 t
如果你不'喜欢下划线)
我正在使用 django,下面的代码运行效率低下。有没有办法通过使用 for 语句来缩短创建和附加列表的方法,如下面的代码所示?列表用于在顶点图表中创建图形 javascript。
[views.py]
annotations = {}
types = ('A', 'B', 'C', 'D', 'E', 'F')
for type in types:
annotations[type] = Count('id', filter=Q(type=type))
annotations[f'r_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Recruiting'))
annotations[f'N_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Not yet recruiting'))
annotations[f'H_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Holding'))
annotations[f'C_{type}'] = Count('id', filter=Q(type=type, is_recruiting='Completed'))
counts = Research.objects.values('teacher').annotate(**annotations).values('teacher', *annotations.keys())
teacher = [];
A = [];
B= [];
C= [];
D= [];
E= [];
F= [];
r_A = [];
r_B = [];
r_C = [];
r_D = [];
r_E = [];
r_F = [];
...
C_E = [];
C_F = [];
for count in counts:
teacher.append(str(count['teacher']))
A.append(str(count['A']))
B.append(str(count['B']))
C.append(str(count['C']))
D.append(str(count['D']))
E.append(str(count['E']))
F.append(str(count['F']))
r_A.append(str(count['r_A']))
r_B.append(str(count['r_B']))
r_C.append(str(count['r_C']))
r_D.append(str(count['r_D']))
r_E.append(str(count['r_E']))
r_F.append(str(count['r_F']))
...
C_E.append(str(count['C_E']))
C_F.append(str(count['C_F']))
return render(request, 'graph.html',
{
'teacher': teacher,
'A': A,
'B': B,
'C': C,
'D': D,
'E': E,
'F': F,
'r_A': r_A,
'r_B': r_B,
'r_C': r_C,
'r_D': r_D,
'r_E': r_E,
'r_F': r_F,
...
'C_E': C_E,
'C_F': C_F
})
[graph.html]
series: [{% if is_recruiting == 'Recruiting' %}
{
name: 'A',
data: {{ r_A | safe }}
}, {
name: 'B',
data: {{ r_B | safe }}
}, {
name: 'C',
data: {{ r_C | safe }}
}, {
name: 'D',
data: {{ r_D | safe }}
}, {
name: 'E',
data: {{ r_E | safe }}
}, {
name: 'F',
data: {{ r_F | safe }}
},{% elif is_recruiting == 'ALL' %}
{
name: 'A',
data: {{ A | safe }}
}, {
name: 'B',
data: {{ B | safe }}
}, {
name: 'C',
data: {{ C | safe }}
}, {
name: 'D',
data: {{ D | safe }}
}, {
name: 'E',
data: {{ E | safe }}
}, {
name: 'F',
data: {{ F | safe }}
}, ... {% elif is_recruiting == 'Completed' %}
{
name: 'A',
data: {{ C_A | safe }}
}, {
name: 'B',
data: {{ C_B | safe }}
}, {
name: 'C',
data: {{ C_C | safe }}
}, {
name: 'D',
data: {{ C_D | safe }}
}, {
name: 'E',
data: {{ C_E | safe }}
}, {
name: 'F',
data: {{ C_F | safe }}
},{% endif %}
],
这是一个使用 apexchart.js 创建图形的 javascript。 这部分也是用if语句继续代码,所以可读性不好。有什么办法可以缩短这部分吗?
类似于:
for count in counts:
teacher.append(str(count['teacher']))
A.append(str(count['A']))
B.append(str(count['B']))
C.append(str(count['C']))
D.append(str(count['D']))
E.append(str(count['E']))
F.append(str(count['F']))
可能是 re-written 使用字典而不是多个列表:
count_dict = defaultdict(list)
for type_, count in itertools.product(types, counts):
count_dict[type_].append(str(count[type_]))
(它是 type_
而不是 type
的原因是因为 type
是一个保留字......只需选择另一个 var 名称,例如 t
如果你不'喜欢下划线)