'The sum of rows from the SQLite table in Python
Git: https://github.com/nikitaborisov00/site/tree/main/gpdf
Tell me how to calculate the sum of all grades for each student.
There is a table: with columns (lesson, teacher, student, grade, total, date).
Here is the code creating the model:
migrations.CreateModel(
name='Grade',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('grades', models.IntegerField(default=0, verbose_name='Оценка')),
('key', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='indexing.lecture', verbose_name='За занятие')),
('key_to_student', models.ManyToManyField(to='indexing.students', verbose_name='Студентам')),
('teacher', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Учитель')),
],
options={
'verbose_name': 'Оценка',
'verbose_name_plural': 'Оценки',
},
),
A piece of code from an html file, table output:
<tbody>
{% for row in table %}
{% for student in row.key_to_student.all %}
<tr>
<td>{{ student.full_name }}</td>
<td>{{ row.key.title }}</td>
<td>{{ row.teacher.get_full_name }}</td>
{% if row.grades == 0 %}
<td>Пропуск</td>
{% else %}
<td id="value">{{ row.grades }}</td>
{% endif %}
<td>
*<script type="text/javascript">
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += document.getElementById("value");
document.write(sum);
</script>*
</td>
<td>{{ row.key.date }}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
