如何在Django模型/视图中计算总和 [英] How to calculate sum in django models/views
问题描述
我正在尝试为学生制作成绩应用程序,有两种模式,一种是针对某一学科的课程,每门课程都有一个以上具有学科学分的学科,因此我想总结所有学科学分取决于课程,现在我可以获得单门课程的总科目学分,但是是否有可能获得所有课程的总科目学分?因为他/她可能有多个课程。例如,一门课程的科目总学分= 12,另一门学分= 8 .......所以总学分= 20学分,请参见图像 ,什么是第二图像模型设计的更好方法
I'm trying to make result app for student, there are two models one is for subject one for course, every course have more than one subject with subject credit, so I want to sum all subject credits depends on the course, now I'm getting single course's total subject credit, but is it possible to have all course's subjects credit total? because he/she could have more than one course. for example one course's subject total credit = 12 another one credit = 8 .......so total = 20 credits please see the images and what will be better approach for second image model design
models.py
class Subject(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=50)
code = models.PositiveIntegerField(unique=True)
credit = models.IntegerField(blank=True)
files = models.FileField(upload_to='course/materials/', blank=True)
status = models.CharField(max_length=15, choices=Subject_Status, blank=True)
def __str__(self):
return self.name
class Meta:
db_table = ''
managed = True
verbose_name = 'Subject'
verbose_name_plural = 'Subjects'
class Course(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=200, unique=True)
prefix = models.CharField(max_length=20)
code = models.CharField(max_length=20)
subject = models.ManyToManyField('Subject', related_name='subject_list', blank=True)
faield = models.ManyToManyField(Subject,related_name='failed_subject_status', blank=True)
passed = models.ManyToManyField(Subject,related_name='passed_subject_status', blank=True)
nerver = models.ManyToManyField(Subject,related_name='never_subject_status', blank=True)
current = models.ManyToManyField(Subject,related_name='curent_subject_status', blank=True)
program = models.ForeignKey('Program', related_name='program_course', on_delete=models.CASCADE,
blank=True, null=True)
views.py
class Program_structure(generic.View):
def get(self, *args, **kwargs):
profile = Student.objects.all()
program_structure = Course.objects.filter(user=self.request.user)
credit =
Course.objects.filter(user=self.request.user).annotate(total_credit=Sum('subject__credit'))
context = {
'test':program_structure,
'credit':credit, #this is giving single course total
'profile':profile,
}
return render(self.request, 'test.html', context)
推荐答案
您可以汇总以下金额的总计贷方
request.user
的课程
的主题
You can sum up the total credit
s of the Subject
s of the Course
s of the request.user
with:
from django.db.models import Sum
total_credit = request.user.course_set.aggregate(
total_credit=Sum('subject__credit')
)['total_credit'] or 0
例如:
from django.db.models import Sum
class Program_structure(generic.View):
def get(self, *args, **kwargs):
profile = Student.objects.all()
program_structure = Course.objects.filter(user=self.request.user)
credit =
Course.objects.filter(user=self.request.user).annotate(total_credit=Sum('subject__credit'))
total_credit = request.user.course_set.aggregate(
total_credit=Sum('subject__credit')
)['total_credit'] or 0
context = {
'test':program_structure,
'credit':credit, #this is giving single course total
'profile':profile,
'total_credit' : total_credit
}
return render(self.request, 'test.html', context)
,然后使用以下命令将其呈现在模板中: / p>
and then render it in the template with:
{{ total_credit }}
这篇关于如何在Django模型/视图中计算总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!